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