最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change the querystring when I submit my GET form using JQuery? - Stack Overflow

programmeradmin2浏览0评论

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 (ment "???") 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;

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 (ment "???") 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;
Share Improve this question asked May 22, 2011 at 11:31 Sandro MundaSandro Munda 41.1k24 gold badges101 silver badges125 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You're almost there. Intercept the submit event (as you are doing), extract the min and max values, build your url and set 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;
    });
});

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.
    });
});

Answer by Rob Cowie is one method. Another one is adding a hidden field named "price" and fill it before submitting it with the value you want.

发布评论

评论列表(0)

  1. 暂无评论