jQuery/Javascript - reload current page with an appended querystring?

后端 未结 6 472
小鲜肉
小鲜肉 2021-02-03 18:40

I\'ve got a dropdown menu on my form, which when something is selected I need to reload the current page, but with an appended querystring.

How would I go about doing th

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 19:09

    var params = [
        "foo=bar",
        "base=ball"
    ];
    
    window.location.href =
        "http://" +
        window.location.host +
        window.location.pathname +
        '?' + params.join('&');
    

    That code within your change event handler will do the trick.

    For instance:

    $('#my_dropdown_id').bind('change', function(){
        var params = [
            "foo=bar",
            "base=" + $(this).val()
        ];
    
        window.location.href = "http://" + window.location.host + window.location.pathname + '?' + params.join('&');
    });
    

提交回复
热议问题