簡體   English   中英

html 表單按鈕鏈接到 url

[英]html form button link to url

我有一個帶有框和按鈕的簡單搜索表單。

<form action = "/search/" method = "get">
<input id = "search_box" type ="text" name = "location" value placeholder = "Where are you?" />
<input id = "search_button" type="submit" value = 'Go' />
</form>

這會將我發送到/search/?location=whatever

我如何才能將其發送到/search/whatever - 即沒有 GET 數據,只有 URL。

您不能像那樣重寫表單發布方法。 一種有效執行此操作的方法是通過根目錄中的.htaccess

RewriteEngine On
RewriteRule ^search\/?location=(.*)$ search/$1

這會將/search/?location=whatever更改為/search/whatever

或者,如果您正在尋找復雜的 JS 解決方案。 這是一個使用 jQuery

$("form").submit(function() {
    var search = $("#search_box").val(); //get the element
    $(this).attr("action", $(this).attr("action")+search);  //attach to the post url
    $("#search_id").remove();  //remove the element, so it doesnot get sent
    console.log($(this).attr('action')); //check the console, if the action was changed and yes it was
    //return false; //continues the post to the new url if commented
});

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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