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

Tan*_*anu 3 html javascript

我有一个 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('');"/> 
Run Code Online (Sandbox Code Playgroud)

请建议我该怎么做。

Stu*_*ser 5

如果事件是由按钮引发的,那么在没有先获取的情况下,您将无法将文本输入对象传递给函数,通过 id 获取它是最好的方法。

您可以使用文本框的 id:

var searchValue = document.getElementById('ad_search_query').value;
Run Code Online (Sandbox Code Playgroud)

在您的函数中,或使用 Ahsan Rathod 的方法将此代码用作参数。