[英]How to change <input type=“text”> value by attribute
I have a program which creates input element by JS ( createElement("INPUT")
). 我有一个通过JS创建输入元素的程序(
createElement("INPUT")
)。
I tried to change the value of the element in runtime by changing the attributes value
and text
but the attribute changed and the value which I saw in the input box stay in the previous value. 我试图在运行时中通过更改属性
value
text
本来更改元素的value
,但是属性已更改,并且在输入框中看到的值保持在先前的值。
how I could to change the value in runtime? 如何在运行时更改值?
You can use .value
to get and change the value property of your newly created element. 您可以使用
.value
来获取和更改新创建的元素的value属性。
Working Snippet Example: 工作片段示例:
//create text input var someInput = document.createElement("INPUT"); someInput.setAttribute("type", "text"); //append element to DOM var myDiv = document.getElementById("someCode"); myDiv.appendChild(someInput); //change input value someInput.value = "New Value"
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Snippet</title> </head> <body> <div id="someCode"> </div> </body> </html>
Changing the value of an input's value as previously shown should work. 如前所述,更改输入值的值应该可以。
the attribute changed and the value which I saw in the input box stay in the previous value.
该属性已更改,并且在输入框中看到的值保持在先前的值。
However, the real answer the poster is looking for is why the value is not changing as expected. 但是,发布者正在寻找的真正答案是为什么价值没有按预期变化。
The reason the values does not change in DevTools is because the value attribute will only show the initial value of the input and not any changes to it. 值在DevTools中不更改的原因是,值属性将仅显示输入的初始值,而不显示输入的任何更改。
value The input control's value.
value输入控件的值。 When specified in the HTML, this is the initial value, and from then on it can be altered or retrieved at any time using JavaScript to access the respective HTMLInputElement object's value property.
在HTML中指定时,这是初始值,此后可以使用JavaScript访问相应的HTMLInputElement对象的value属性随时对其进行更改或检索。 The value attribute is always optional.
value属性始终是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.