我有这个表格要处理..
<form action="driver.php" method="GET">
<input type="text" placeholder="Search" id="search" name="n">
<button type="submit">Submit</button>
</form>
默认情况下,url 是 http://sites.com/driver.php?n=typedname
所以我在 .htaccess 中进行了修改并将其转换为 http://sites.com/driver/typedname
以获得 SEO 友好的 url modrewrite issue - 404 error感谢 anubhava
一切都很好,但现在问题来了,当我输入并单击提交按钮时,它将转到 http://sites.com/driver.php?n=typedname
那么我怎样才能让它在点击提交后转到这个 url http://sites.com/driver/typedname
而不是?
我认为 javascript 可以做到这一点,所以我标记了它,希望我没有错。
谢谢。
最佳答案
jQuery
$('button[type=submit]').click(function(e){
e.preventDefault();
e.stopPropagation();
window.location = "http://sites.com/driver/" + $('#search').val();
return false;
});
或
$('form').submit(function(e) { // you really need to class/ID your form
// all that code
});
现在给你这个想法是快速而肮脏的。您当然想要清理您的输入(在前端和后端都这样做是个好主意)。
关于javascript - 如何在提交时更改表单 url 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409950/