使用 JQuery 提交 GET 表单时如何更改查询字符串? [英] How to change the querystring when I submit my GET form using JQuery?
本文介绍了使用 JQuery 提交 GET 表单时如何更改查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我的页面中有一个简单的表单,如下所示:
Suppose that I have a simple form in my page like this :
<form action="/properties/search" method="GET" id="form_search">
<p>
<label for="price">Min price:</label>
<input type="text" name="min_price" id="min_price">
</p>
<p>
<label for="price">Max price:</label>
<input type="text" name="max_price" id="max_price">
</p>
<p>
<input type="submit">
</p>
</form>
当我提交表单时,我有以下网址:
When I submit my form, I have the following url :
http://.../properties/search?min_price=100000&max_price=200000
我想改变这个网址:
http://.../properties/search?price=100000,200000
为此,我使用了 JQuery 和 JQuery 查询字符串插件:
To do that, I'm using JQuery and the JQuery querystring plugin :
$(document).ready(function() {
$("#form_search").submit(function() {
var querystring = rewrite_interval_qstring();
// querystring equals "?price=100000,200000" -> exactly what I want !
// ???
});
});
如何更改(评论???")提交网址?我已经单独测试了以下说明,但它不起作用.
How can I change (comment "???") the submit url ? I have tested the following instructions separately, but it does not work.
window.location = querystring;
window.location.href = querystring;
window.location.search = querystring;
推荐答案
你需要阻止默认提交动作的发生
You need to prevent the default submit action from happening
$(document).ready(function() {
$("#form_search").submit(function(event) {
event.preventDefault(); // <-- add this
var querystring = rewrite_interval_qstring();
// querystring equals "?price=100000,200000" -> exactly what I want !
window.location.href = querystring; // <-- this should work.
});
});
这篇关于使用 JQuery 提交 GET 表单时如何更改查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文