简体   繁体   English

如何将html文本框值传递给javascript函数

[英]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">&nbsp;
<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">&nbsp; 
<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">&nbsp; 
<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.

相关问题 如何将Textbox值传递给Javascript函数? - How to pass Textbox value to Javascript function? 如何将文本框(不是文本框的值)传递给Javascript验证功能 - How to pass a textbox (not the value of textbox) to Javascript validate function 如何在文本框中输入值(嵌套在表单的HTML表中)并将其传递给Javascript函数 - How to enter a value in a textbox (nested in a HTML table which is in a form) and also pass it to a Javascript function 如何在单击按钮时将HTML文本框的值(内容)传递给javascript函数? - How can I pass value(contents) of HTML textbox to a javascript function on a button click? 如何在 html/javascript 上创建动态文本框时保存一个值并传递它 - How to hold a value and pass it while creating dynamic textbox on html/javascript 如何将文本框值传递给JavaScript? - How to pass textbox value to JavaScript? 如何传递价值 <asp:textbox> 到javascript函数 - How to pass value from <asp:textbox> to javascript function 将文本框值传递给 JavaScript 中的编辑功能 - Pass textbox value to edit function in JavaScript 如何将值从 JavaScript 中的函数返回到 HTML 中的文本框 - How do I return a value from a function in JavaScript to a textbox in HTML 使用javascript函数设置HTML文本框值? - Setting HTML textbox value using javascript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM