簡體   English   中英

從定位標記href URL調用js函數

[英]Calling js function from the anchor tag href 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>

我必須在錨標記url中使用輸入的文本作為searchname參數。

我怎樣才能做到這一點?

我會嘗試用HTML正確標記事物,以避免無用的JS。

<form method="GET" action="https:...">
<input type="text" name="searchname"/>
<button type="submit">Go!</button>
</form>

如果input不在form標記之外,則可以使用form屬性(請參閱HTML生活規范https://html.spec.whatwg.org/multipage/forms.html#the-input-element

只需調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM