假设我的页面中有一个像这样的简单表单:
<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>
当我提交表单时,我有以下网址:
http://.../properties/search?min_price=100000&max_price=200000
我想将此 url 更改为:
http://.../properties/search?price=100000,200000
为此,我使用了 JQuery 和 JQuery querystring plugin :
$(document).ready(function() {
$("#form_search").submit(function() {
var querystring = rewrite_interval_qstring();
// querystring equals "?price=100000,200000" -> exactly what I want !
// ???
});
});
如何更改(评论“???”)提交网址?我分别测试了下面的指令,但是都不行。
window.location = querystring;
window.location.href = querystring;
window.location.search = querystring;
最佳答案
你快到了。拦截提交事件(正如你所做的那样),提取最小值和最大值,构建你的 url 并设置 window.location.href
$(document).ready(function() {
$("#form_search").submit(function(event) {
event.preventDefault();
$this = $(this);
// var url = rewrite_interval_qstring();
var min_price = $('#min_price').val();
var max_price = $('#max_price').val();
var url = $this.attr('action') + '?price=' + min_price + ',' + max_price;
window.location.href = url;
});
});
关于javascript - 使用 JQuery 提交 GET 表单时如何更改查询字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087634/