Change URL parameters

前端 未结 26 2661
孤独总比滥情好
孤独总比滥情好 2020-11-22 08:40

I have this URL:

site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc

what I need is to be able to

26条回答
  •  面向向阳花
    2020-11-22 09:02

    I wrote a little helper function that works with any select. All you need to do is add the class "redirectOnChange" to any select element, and this will cause the page to reload with a new/changed querystring parameter, equal to the id and value of the select, e.g:

    
    

    The above example would add "?myValue=222" or "?myValue=333" (or using "&" if other params exist), and reload the page.

    jQuery:

    $(document).ready(function () {
    
        //Redirect on Change
        $(".redirectOnChange").change(function () {
            var href = window.location.href.substring(0, window.location.href.indexOf('?'));
            var qs = window.location.href.substring(window.location.href.indexOf('?') + 1, window.location.href.length);
            var newParam = $(this).attr("id") + '=' + $(this).val();
    
            if (qs.indexOf($(this).attr("id") + '=') == -1) {
                if (qs == '') {
                    qs = '?'
                }
                else {
                    qs = qs + '&'
                }
                qs = qs + newParam;
    
            }
            else {
                var start = qs.indexOf($(this).attr("id") + "=");
                var end = qs.indexOf("&", start);
                if (end == -1) {
                    end = qs.length;
                }
                var curParam = qs.substring(start, end);
                qs = qs.replace(curParam, newParam);
            }
            window.location.replace(href + '?' + qs);
        });
    });
    

提交回复
热议问题