How to change the querystring when I submit my GET form using JQuery?

后端 未结 3 1286
抹茶落季
抹茶落季 2020-12-19 12:25

Suppose that I have a simple form in my page like this :

相关标签:
3条回答
  • 2020-12-19 12:55

    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;
        });
    });
    
    0 讨论(0)
  • 2020-12-19 12:56

    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 讨论(0)
  • 2020-12-19 13:03

    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.
        });
    });
    
    0 讨论(0)
提交回复
热议问题