?name=1&id=7&value=xx大概這樣的
怎么切割鍵值對,求大神給寫個出來!
19990000
2016-11-21 16:45:00
TA貢獻446條經驗 獲得超754個贊
var?query?=?'?name=1&id=7&value=xx';
query?=?query.substring(1);
var?reg?=?/(?!&)([^=]*)=([^&]*)/g,?result,?queryObj={};
while(result=reg.exec(query))?{
queryObj[decodeURIComponent(result[1])]?=?decodeURIComponent(result[2]);
}
console.log(queryObj);
TA貢獻178條經驗 獲得超70個贊
/**
?*?獲取URL參數值
?*?@type?普通模式?pathinfo模式
?*?@param?param?參數名
?*?@returns?{*}
?*/
function?getUrlParam(param)
{
????if?(!param)?return?null;
????var?reg?=?new?RegExp("(^|&|/)"?+?param?+?"[=|/]([^&|/]*)(&|/|$)");?//構造一個含有目標參數的正則表達式對象
????var?r?=?window.location.search.substr(1).match(reg);??//匹配目標參數
????if?(r?!=?null)?return?decodeURI(r[2]);?return?null;?//返回參數值
}舉報