html表单按钮链接到url

时间:2012-04-08 11:01:42

标签: html

我有一个带有方框和按钮的简单搜索表单。

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

1 个答案:

答案 0 :(得分:1)

你无法重写那样的表单发布方法。有效地执行此操作的方法是通过根目录.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
});

Demo