問題:關於貼友一個用js傳遞value預設值的簡單實現,jsvalue

來源:互聯網
上載者:User

問題:關於貼友一個用js傳遞value預設值的簡單實現,jsvalue

需求和代碼如下:

“這是My Code:”

   1: <input type="text" id="price2" value="333"/>
   2: <input type="text" id="trueprice" value="" />
   3:
   4:
   5: <script type="text/javascript">
   6: document.getElementById("price2").onkeyup = function() {
   7: document.getElementById("trueprice").value = this.value;
   8: }
   9: </script>

問題:現在開啟這個頁面,trueprice的值預設是空的,怎樣才能實現預設開啟這個頁面trueprice就已經和price2一樣了呢?(price2是一個動態數值)
<input type=”text” id=”trueprice” value=”” />是固定不可以修改的

 

我的一個簡單實現:

   1: <!DOCTYPE HTML>
   2: <html>
   3:     <head>
   4:         <meta http-equiv="content-type" content="text/html;charset=utf-8">
   5:         <title>Test</title>
   6:     </head>
   7:     <body>
   8:    <input type="text" id="price2" value="333"  onkeyup="test(this.value);"/>
   9:    <input type="text" id="trueprice" value="" />
  10:     <script type="text/javascript">
  11:         var price2 = document.getElementById("price2").value;
  12:         document.getElementById("trueprice").value = price2;
  13:         function test (defaultVal) {
  14:           document.getElementById("trueprice").value = defaultVal;
  15:         }
  16:     </script>
  17:
  18:     </body>
  19: </html>

效果:


在第一個文字框中輸入的內容能同步到第二個文字框

 

若你有不同意見,歡迎交流。不足之處,敬請諒解。

來源:http://www.ido321.com/567.html

關於js中設定預設參數,可以戳此:http://www.ido321.com/555.html




怎用JS保留提交表單後的value值

你這情況應該是提交動作會postback到幕後處理是吧。 頁面重新整理後 文字框中的內容就都沒有了。
這種情況有兩種方式解決:
1. 用後台代碼: 服務端代碼 變數來儲存這些值 舉個c#的例子。
<input type="text" value="<%= entity.time%>" id="date"/>
在執行提交前, 給entity這個實體賦值, 這樣就能儲存住了。
2. 頁面回填的方式:
session Storage只是舉個例子
在執行提交前, 將頁面上的文字框內容儲存在全域變數中,如【session Storage】等。 每次js在window.onload執行時都去判斷【session Storage】中有沒有值, 有就回填。
3. 市面上有一些架構支援頁面回填的。 你可以找找。
 
js中,一個使用者的文字框,我想讓這個文字框預設值就是123,密碼也是預設寫好的,怎實現

value="123" 就是定義一個初始值
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.