[英]Calling js function from the anchor tag href url
I have a search box with search button. 我有一个带有搜索按钮的搜索框。 When user enters the text in search box and click on search button, it should redirect the url by passing inputed text in search box as an parameter in url.
当用户在搜索框中输入文本并单击搜索按钮时,应该通过在搜索框中传递输入的文本作为url中的参数来重定向url。
#input tag for search box
<input type="search" placeholder="search" class="search_3">
<a href="https:.........?searchname=">
#input tag for search button
<input type="submit" class="submit_3" value="Search"/>
</a>
i have to use the inputed text for searchname parameter in the anchor tag url. 我必须在锚标记url中使用输入的文本作为searchname参数。
How can i achieve this? 我怎样才能做到这一点?
I would try to HTML tag things properly, to avoid useless JS. 我会尝试用HTML正确标记事物,以避免无用的JS。
<form method="GET" action="https:...">
<input type="text" name="searchname"/>
<button type="submit">Go!</button>
</form>
You can use form
attribute if the input
is outside the form
tag (see HTML living spec https://html.spec.whatwg.org/multipage/forms.html#the-input-element ) 如果
input
不在form
标记之外,则可以使用form
属性(请参阅HTML生活规范https://html.spec.whatwg.org/multipage/forms.html#the-input-element )
Just call onlclick your js method and get the search field value and redirect using js code.You can try like this : 只需调用onlclick您的js方法并获取搜索字段值并使用js代码进行重定向即可。您可以这样尝试:
<input type="search" placeholder="search" class="search_3" id="search_3">
<button name="redirect" onClick="redirecturl()" class="submit_3" value="Search">
<script type="text/javascript">
function redirecturl()
{
window.location.href = "http://www.example.com/?searchname="+document.getElementById("search_3").value;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.