1.通过正则:
function getQueryStr(str){ var rs = new RegExp("(^|)"+str+"=([^\&]*)(\&|$)","gi").exec(LocString), tmp; if(tmp=rs){ return tmp[2]; } // parameter cannot be found return ""; } id = getQueryStr("id");
2.用jquery先获得url,再拆分字符串
// var LocString=String(window.document.location.href); 获得url var url = location.search; //获取url中"?"符后的字串 var id = null; if(url != null && url.indexOf("?") != -1){ var index = url.indexOf("id="); var start = 0; if (index != -1) { start = index + 3; } var end = url.indexOf("&", start); if (end == -1) { end = url.length; } id = url.substring(start, end); // 获取get方式传来参数中的id对应值 $("#id").val(id); }