let a = document.querySelector('#b1').value;
console.log(`a: ${a}`);
document.querySelector('#b1').value = 10;
console.log(`a: ${a}`);
uj5u.com熱心網友回復:
正如評論中所說,
當您這樣做時,let a = document.querySelector('#b1').value; 它會決議該值并將其保存在變數 a 中。
這樣做,它不會被更新。要獲得實際值,您需要每次都獲得它。
像這樣:
let wrongWay = document.getElementById("foo").value; // Getting it the wrong way
let rightWay = document.getElementById("foo"); // Getting it the right way
console.log("wrong way value : " wrongWay);
console.log("right way value : " rightWay.value);
// Changing input value
document.getElementById("foo").value = "new value";
console.log("Changed value !");
console.log("wrong way value : " wrongWay); // The value isn't updated as it is saved as a string -> so it didn't get any update
console.log("right way value : " rightWay.value); // By getting it, you are sure to get the right value
<input id="foo" type="text" value="old value" >
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327703.html
標籤:javascript dom
上一篇:如何將多個JavaScript物件陣列填充到HTMLDOM
下一篇:根據選擇更新欄位
