This question already has answers here:
How can I get query string values in JavaScript?
                            
                                (73个答案)
                            
                    
                6年前关闭。
        

    

我有这样的网址:

http://example.com/mypage?vendorId=1&vendorId=2&vendorId=3


使用jQuery,如何获取每个vendorId值并将它们放入数组中?

最佳答案

在javascript中,这只是一个字符串:

var url = 'http://mysite.com/mypage?vendorId=1&vendorId=2&vendorId=3'
var qs = url.split('?')[1];
var parts = qs.split('&');
var arr = [];

$.each(parts, function() {
    var val = this.split('=')[1];
    arr.push(val);
});


FIDDLE

或简短的方法:

var arr = $.map(window.location.split('?')[1].split('&'), function(e,i) {
    return e.split('=')[1];
});

10-07 17:52