簡體   English   中英

添加事件以更改輸入值

[英]Add event to change input value

我正在嘗試創建一個函數來更改 HTML 腳本中的輸入值。 HTML 看起來像這樣:

<tr>
   <td>6500</td>
   <td>
      <input type="text" size="3" value="3"/>
  </td>
</tr>
<tr>
   <td>6500</td>
   <td>
      <input type="text" size="3" value="3"/>
  </td>
</tr>

我想創建一個事件,其中值根據用戶輸入更新並更改值,以便當我單擊按鈕時它執行我已經為其編寫函數的計算。 我被卡住了,我唯一擁有的是用戶輸入是一個選項的單元格。 我需要做簡單的 js 而不是 jquery。

function input() {
var table = document.getElementById("list");
var x = table.getElementsByTagName("input");

console.log(x);

}

提前致謝。

不知道為什么我在你的跟進評論后從來沒有回復過,我知道這已經太晚了,但如果這不再幫助你,它可能仍然幫助其他人。

使用 JavaScript:

var inputs = document.querySelectorAll('input');
for (var i = 0; i < inputs.length; i++) {
  inputs[i].addEventListener('input', function() {
    this.setAttribute('value', this.value);
  });
}

使用jQuery:

$(document).on('input', 'input', function() {this.setAttribute('value', this.value);});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM