简体   繁体   English

如何改变 <input type=“text”> 按属性值

[英]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. 但是,发布者正在寻找的真正答案是为什么价值没有按预期变化。

Attribute shows initial value 属性显示初始值

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属性始终是可选的。

See MDN . 参见MDN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将HTML输入类型属性从“密码”更改为“文本”? - How to change the HTML input type attribute from 'password' to' text'? 编辑输入类型时如何更改value属性? - How to change the value attribute when I edit Input type? 为什么在更新输入类型文本的值时敲除不更改value属性? - Why does knockout not change the value attribute when updating value of input type text? 更改属性文本并通过敲除保留输入的值 - Change attribute text and keep value of an input with knockout 如果在不触摸此输入的情况下从另一个元素插入值,如何处理输入类型为“ text”的输入的更改或输入事件 - How to handle change or input event of input type=“text” if value is inserted from another element without touching this input 除非type =“ hidden”,否则输入字段值属性不会更改 - input field value attribute wont change unless type=“hidden” 如何将按钮的数据属性设置为输入[type = text]中设置的任何值? - How to set the data-attribute of a button to whatever value set in an input [type=text]? 如何使用多维名称属性更改html输入文本框名称的值 - How to change value of html input text box name with multidimenal name attribute 使用 Javascript 或 Jquery 更改 span 的值<input>或文本属性 - Change the value of span using Javascript or Jquery using <input> or text attribute 使用文本输入值即时更改链接href属性 - Change link href attribute on the fly with text input value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM