jq多级分类筛选url的拼接方法

作者:有用网 阅读量:340 发布时间:2021-12-01
关键字 HTML JavaScript 前台
这个方法绝对好用,亲测有效    
$(".url_click").on('click', function() {
        var url = '';      
        var ntype = $(this).attr('ntype');//参数名
        var nvalue = $(this).attr('nvalue');//参数值

        if (ntype && nvalue) {
            url = UpdateUrlParam(ntype, nvalue)
        }
        window.location.href = url;
    });

    function UpdateUrlParam(name, val) {
        var thisURL = document.location.href;
        // 如果 url中包含这个参数 则修改
        if (thisURL.indexOf(name + '=') > 0) {
            var v = getQueryString(name);
            if (v != null) {
                // 是否包含参数
                thisURL = thisURL.replace(name + '=' + v, name + '=' + val);

            }
            else {
                thisURL = thisURL.replace(name + '=', name + '=' + val);
            }

        } // 不包含这个参数 则添加
        else {
            if (thisURL.indexOf("?") > 0) {
                thisURL = thisURL + "&" + name + "=" + val;
            }
            else {
                thisURL = thisURL + "?" + name + "=" + val;
            }
        }
        return thisURL;

    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }




#发表评论
提交评论