javascript 动态更改输入文本字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20507581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:38:36  来源:igfitidea点击:

Dynamically change value of input textField

javascripthtmlinput

提问by user2005121

I have a html block as follows with an input textfield that updates the entire application when it is manually edited.

我有一个 html 块,如下所示,其中包含一个输入文本字段,在手动编辑时会更新整个应用程序。

How can I dynamically target and change this input text field? In the following this would be value="2"

如何动态定位和更改此输入文本字段?在下面这将是 value="2"

    <my-attribute param="Count" min="0" max="100" step="1">
        <span id="label">Count</span><span id="input">
<dat-input-number id="input" value="2" step="1" min="0" max="100">
         <div id="blocker" on-dblclick="" style="width: 152px; height: 15px;"></div>
        <input id="number" type="text" on-keydown="">       
      </dat-input-number></span>
      </my-attribute>

回答by EvilBeer

<script>
document.getElementById('number').value = "yourValueHere";
</script>

Oh, and don't forget to close your <input id="number" type="text" on-keydown="">with an </input>

哦,别忘了<input id="number" type="text" on-keydown=""></input>

回答by The Muffin Man

<script>
    var theInput = document.getElementById('input');
    theInput.value = '5';
</script>

Working demo here

工作演示在这里

Note: This is super trivial and I have no doubt that you could have found enough knowledge through a Google search to attempt this on your own. :)

注意:这是非常微不足道的,我毫不怀疑您可以通过 Google 搜索找到足够的知识来自己尝试。:)