[英]How do I input the value in input field using 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>
使用class
:
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>
也許是這樣的:
document.getElementsByClassName('contacts_modal_search_field')[0].value = "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>
您可以使用java腳本在文本框中輸入。
<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="" id='contacts_modal_search_field'>
<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>
<script>
window.onload=function(){
//by Class name
document.getElementsByClassName('contacts_modal_search_field')[0].value='Something'
//by Id
document.getElementById('contacts_modal_search_field')[0].value='Something' //if 'contacts_modal_search_field' is your input box ID
//By Tag name
document.getElementsByTagName('input')[0].value='Something'
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.