我想使用 JQuery 从 href 属性字符串中提取值
$(this).attr("href")
会给我
?sortdir=ASC&sort=Vendor_Name
我需要的是将这些值解析成一个数组
myArray['sort']
myArray['sortdir']
有什么想法吗? 谢谢!
顺便说一句,我在 SO 的其他地方看到了以下与查询字符串一起使用的类似想法。 我还不能根据我的需要调整它。
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
最佳答案
尝试以下操作:
var href = $(this).attr("href")
href = href.replace('?', "").split('&');
var myArr = {};
$.each(href, function(i, v){
var s = v.split('=');
myArr[s[0]] = s[1];
});
关于javascript - 使用 JQuery 从 href 属性字符串中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690267/