[英]How to pass html textbox value to a javascript function
I have a HTML textbox where user will input some string which i want to pass to a JavaScript function: 我有一个HTML文本框,用户将输入一些我想传递给JavaScript函数的字符串:
<input type="text" id="ad_search_query" style="width:295px">
<input type="button" value="<s:text name="button.search"/>" class="p-userButton"
onClick="ADSEARCHGF.showTable('');"/>
Please suggest how can i do it. 请建议我该怎么做。
If the event is raised by the button, you will not have the text input object to pass to the function without getting it first, getting it by id is the best way. 如果按钮引发了事件,则不会先将文本输入对象传递给函数而不先获取它,通过id获取它是最好的方法。
You can use the id of the text box: 您可以使用文本框的ID:
var searchValue = document.getElementById('ad_search_query').value;
in your function, or use Ahsan Rathod's method to use this code as parameter. 在你的函数中,或使用Ahsan Rathod的方法将此代码用作参数。
<script>
function showTable(obj){
alert(obj.value)
}
</script>
<input type="text" id="ad_search_query" style="width:295px">
<input type="button" value="search" class="p-userButton" onClick="showTable(document.getElementById('ad_search_query'));"/>
Do this: 做这个:
JS: JS:
<script>
function showTable(val) {alert(val);}</script>
<script>
function showTable(val){alert(val);}</script>
HTML: HTML:
<input type="text" id="ad_search_query" style="width:295px">
<input type="button" value="search" class="p-userButton" onClick="showTable(document.getElementById('ad_search_query').value);"/>
See this Demo: http://jsfiddle.net/rathoreahsan/8ddbD/ 看这个演示: http : //jsfiddle.net/rathoreahsan/8ddbD/
In javascript function you can access textbox value like this 在javascript函数中,您可以像这样访问文本框值
var text = document.getElementsByName("textbox1").value; var text = document.getElementsByName(“textbox1”)。value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.