javascript - 如何使用 JavaScript 在输入字段中输入值?

标签 javascript

我正在尝试使用 javascript 将一些文本添加到输入搜索字段

我尝试了以下代码:

document.getElementsByClassName('contacts_modal_search_field')[0].innerHTML = \"test\

还有很多其他的,但都没有成功。

这是代码:

<div class="contacts_modal_search">
  <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="Search" ng-model="search.query" autocomplete="off" style="">
  <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
    <i class="icon icon-search-clear"></i>
  </a>
</div>

最佳答案

使用 document.getElementById("id").value = "yourvalue"document.getElementsByClassName("yourclass")[index].value = "yourvalue" .

使用id:

Enter text to be the value of the second input field:<br/>
<input type="text" id="text"/>
<br/>
<input type="button" onClick="changeValue()" value="Change Value"/>
<p/>
<input type="text" id="result"/>
<script>
function changeValue(){
 document.getElementById("result").value = document.getElementById("text").value;
}
</script>

使用:

Enter text to be the value of the second input field:<br/>
    <input type="text" class="text"/>
    <br/>
    <input type="button" onClick="changeValue()" value="Change Value"/>
    <p/>
    <input type="text" class="result"/>
    <script>
    function changeValue(){
     document.getElementsByClassName("result")[0].value = document.getElementsByClassName("text")[0].value;
    }
    </script>

对于您的代码:

Enter text to be the value of the search input field:<br/>
    <input type="text" class="text"/>
    <br/>
    <input type="button" onClick="changeValue()" value="Change Value"/>
    <p/>
    <script>
    function changeValue(){
     document.getElementsByClassName("form-control")[0].value = document.getElementsByClassName("text")[0].value;
    }
    </script>
<hr>
<div class="contacts_modal_search">
  <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="Search" ng-model="search.query" autocomplete="off" style="">
  <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
    <i class="icon icon-search-clear"></i>
  </a>
</div>

在窗口加载时更改输入框的值(使用window.onload):

        <script>
        window.onload = function(){
         document.getElementsByClassName("form-control")[0].value = "your value";
        }
        </script>
    <div class="contacts_modal_search">
      <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="" ng-model="search.query" autocomplete="off" style="">
      <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
        <i class="icon icon-search-clear"></i>
      </a>
    </div>

关于javascript - 如何使用 JavaScript 在输入字段中输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325756/

相关文章:

javascript - 如何制作搜索属性值然后根据用户输入隐藏或显示元素的搜索框?

javascript - 如何获取指定为 &lt;script&gt; 标记的 'src' 的文件的内容?

javascript - 我的网页加载,然后立即消失。在 chrome 开发工具中,没有错误的请求。

javascript - 如何将 Canvas 恢复到另一个函数中保存的状态?

javascript - 如何在 Angular 5 中使用 Material 步进器

javascript - 当用户单击图像时传递图像名称或图像 id 属性

javascript - 把东西放在自己的可滚动盒子里

javascript - 如何取消选择文本字段的文本?

JavaScript 条件评估结果为 false?

javascript - 如何覆盖/拦截console.log并将原始日志输出到控制台