简体   繁体   中英

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.

#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.

How can i achieve this?

I would try to HTML tag things properly, to avoid useless 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 )

Just call onlclick your js method and get the search field value and redirect using js code.You can try like this :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM