이 간단한 목록javascript와 Jquery를 사용하여 url 변수를 검색하는 기능? 내 스크립트에
<ul>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
</ul>
내가 변수 ID를 읽을 수있다 필요 그래서 난 아약스 호출을 통해 보낼 수 있습니다 편집 링크를 보여줍니다 나는이 기능을 시도
$(document).ready(function(){
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1)
.split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
$('.edit').click(function(){
var test = getUrlVars()["id"];
alert(test);
});
});
링크를 클릭하면 경고 메시지에 undefined
이 표시됩니다.
$(document).ready(function(){
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]);
})();
$('.edit').click(function(){
var test = urlParams["id"];
alert(test);
});
});
을 ...하지만 심지어이 또한 경고 메시지 undefined
을 보여줍니다.
'vars.push (hash [0]); vars [hash] [0] = hash [1]; - 무엇 때문에 왜 배열과 객체처럼 vars를 처리하려고합니까? – raina77ow
정확하게 @ raina77ow - 방금이 함수를 사용하려고하는 코드를 검토 한 결과 똑같은 것으로 나타났습니다. 나쁜 코드는 내가 생각한 4 년 후에도 물지 않습니다.) – kamituel