How to change the request when I submit my GET form using jQuery?

Suppose I have a simple form on my page:

<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

For this, I use jQuery and 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 sending address? I tested the following instructions separately, but it does not work.

window.location = querystring;
window.location.href = querystring;
window.location.search = querystring;
+5
source share
3 answers

You need to prevent the default send action

$(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.
    });
});
+2

. ( ), min max, 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;
    });
});
+6

The answer to this question is Rob Cowie is one of the methods. Another adds a hidden field called "price" and fills it before sending it with the desired value.

+1
source

All Articles