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

后端 未结 6 471
小鲜肉
小鲜肉 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 18:51

    If you want a really simple way to preserve the query string and possibly append to it, use window.location.search; here's a snippet:

    var search = window.location.search + (window.location.search ? "&" : "?");
                    search += "param1=foo¶m2=bar";
                    window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + search;
    

    You can, of course, use a more sophisticated way of building the rest of your query string, as found in the other examples, but the key is to leverage Location.search.

提交回复
热议问题