当前位置: 动力学知识库 > 问答 > 编程问答 >

executing jquery code after ajax is not working

问题描述:

i'm having an ajax call that prints some links in the page. Than I want to click on any 1 of these links to do something like alert but it's not working

here's the document.ready code

setTimeout(getFur(),500);

$("#curFur a").on("click",function() {

alert("clicked");

});

and the getFur function (php variables work without any problem because there's some php queries and the there's no problem with the ajax)

var ajax = false;

ajax = new XMLHttpRequest();

var qst = "?roomid=<?=$roomID?>&lang=<?=$lang?>";

ajax.open("POST","ajax/curfur.php"+qst);

ajax.onreadystatechange = function(){

if(ajax.readyState == 4 && ajax.status == 200){

$("#curFur").html(ajax.responseText);

}

}

ajax.send(null);

the problem now is clicking the links do nothing at all i've even tried to remove these links but didn't work

$("#curFur a").remove();

网友答案:

Since your anchors has been added to the DOM dynamically by an AJAX call, you need to use event delegation to attach events such as click in your case to these newly added anchors:

$('#curFur').on("click",'a',function() {
    alert("clicked");
});
网友答案:

getFur() it executes immediately

setTimeout(getFur(),500);

Change to this

setTimeout(getFur,500);

or

   setTimeout(function() {
       getFur();

  },500);
分享给朋友:
您可能感兴趣的文章:
随机阅读: