当我使用JQuery提交GET表单时如何更改查询字符串? [英] How to change the querystring when I submit my GET form using JQuery?

查看:191
本文介绍了当我使用JQuery提交GET表单时如何更改查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的页面中有一个简单的表单,如下所示:

 < form action =/ properties / search method =GETid =form_search> 
< p>
< label for =price>最低价格:< / label>
< input type =textname =min_priceid =min_price>
< / p>
< p>
< label for =price>最高价格:< / label>
< input type =textname =max_priceid =max_price>
< / p>
< p>
< input type =submit>
< / p>
< / form>

当我提交表单时,我有以下网址:



http://.../properties/search?min_price = 100000& max_price = 200000



我想将此网址更改为:

< a href =http://.../properties/search?price = 100000,200000 =noreferrer> http://.../properties/search?price = 100000,200000



为此,我使用JQuery和 jQuery querystring插件

  $(document).ready(function(){
$ (#form_search)。submit(function(){
var querystring = rewrite_interval_qstring();
// querystring equals?price = 100000,200000 - >正是我想要的!

// ???
});
});

如何更改(评论???)提交网址?我已经单独测试了以下指令,但它不起作用。

  window.location = querystring; 
window.location.href = querystring;
window.location.search = querystring;


解决方案

您需要防止发生默认提交操作

  $(document).ready(function(){
$(#form_search)。submit(function事件){
event.preventDefault(); //< - 添加此
var querystring = rewrite_interval_qstring();
// querystring等于?price = 100000,200000 - > ;正是我想要的!

window.location.href = querystring; //< - 这个应该可以。
});
});


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

I want to change this url to have :

http://.../properties/search?price=100000,200000

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆