简体   繁体   English

Jquery click函数不适用于动态元素

[英]Jquery click function is not working for dynamic elements

I am using $.each to create the button with each array object. 我正在使用$.each为每个数组对象创建按钮。 I also tried to give each button a specific id, so I can do click event for further coding, but now I don't know why all the buttons are not functioning. 我还尝试给每个按钮一个特定的id,所以我可以点击事件进行进一步编码,但现在我不知道为什么所有按钮都不起作用。 Did I miss some code? 我错过了一些代码吗?

 var questlist = [{ "startdate": "2015-01-08", "questitem": [ { "questid": "1", "gifttype": "stars", "quantity": 10, "questname": "One", "queststatus": "done" }, { "questid": "2", "gifttype": "stars", "quantity": 50, "questname": "Two", "queststatus": "ready" }, { "questid": "3", "gifttype": "stars", "quantity": 100, "questname": "Three", "queststatus": "complete" }, { "questid": "4", "gifttype": "stars", "quantity": 120, "questname": "Four", "queststatus": "done" }, { "questid": "5", "gifttype": "stars", "quantity": 220, "questname": "Five", "queststatus": "ready" }, ] }]; questitemlist(questlist); function questitemlist() { var callquest = "<div class='questlist_container'>" + "<div id='call_questitem'></div>" + "</div>"; $("#call_quest").append(callquest); var questlistobj = questlist[0].questitem; $.each(questlistobj, function(i, obj) { if (obj.queststatus == "ready") { var questlist_item_button = "<input type='button' id='questlist_item_button_go" + obj.questid + "' class='questlist_item_button' id='questlist_item_button' value='GO !'/>"; $("#questlist_item_button_go" + obj.questid).click(function() { alert("go"); }); console.log("#questlist_item_button_go" + obj.questid); } else if (obj.queststatus == "done") { var questlist_item_button = "<input type='button' id='questlist_item_button_reward" + obj.questid + "' class='questlist_item_button' id='questlist_item_button' value='REWARD !'/>"; $("#questlist_item_button_reward" + obj.questid).click(function() { alert("reward"); }); } else if (obj.queststatus == "complete") { var questlist_item_button = "<label class='questlist_item_complete'><img class='questlist_item_img' src='img/check.png'/></label>"; } var questlist_item = "<div class='questlist_item'>" + questlist_item_button + "<label class='questlist_item_questname'>" + obj.questname + "</label>" + "<label class='questlist_item_gifttype'>" + obj.gifttype + " " + obj.quantity + " " + "</label>" + "</div>"; $("#call_questitem").append(questlist_item); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="call_quest"></div> 

The click() function you can call on the elements which have direct binding . 您可以在具有直接绑定的元素上调用click()函数。 Direct binding will only attach event handler which are present at the time of DOM loading ie static elements. 直接绑定将仅附加在DOM加载时存在的事件处理程序,即静态元素。

If there are elements created after DOM is loaded, then not all the events associated with them would be triggered if you have not attached the event handlers correctly. 如果在加载DOM之后创建了元素,那么如果没有正确附加事件处理程序,则不会触发与它们关联的所有事件。

And when you create dynamic elements that means they are being created after DOM is loaded and they were not present at the time of direct binding , so you can not call directly click() on that. 当你创建动态元素时,意味着它们是在加载DOM之后创建的,并且在直接绑定时它们不存在,所以你不能直接调用click()

if you want to get click functionality on dynamic created elements, you'll have create a delegated binding by using on . 如果要在动态创建的元素上获得click功能,则可以使用on创建委派绑定 This you can achieve by adding a .on handler to a static parent element. 这可以通过向静态父元素添加.on处理程序来实现。

Delegated events have the advantage that they can process events from descendant elements that          
are added to the document at a later time.

Change this line 改变这一行

$("#questlist_item_button_reward" + obj.questid).click(function() {
    alert("reward");
});

to

$("#call_questitem").on("click", "#questlist_item_button_reward" + obj.questid, function() {
    alert("reward");
});

And do the same for the go button as well. 并为go按钮执行相同操作。

DEMO DEMO

You're overwriting the dynamic id with questlist_item_button . 您正在使用questlist_item_button覆盖动态ID。

<input 
    type='button' 
    id='questlist_item_button_go"+obj.questid+"'  
    class='questlist_item_button' 
    id='questlist_item_button' <!-- REMOVE ME --> 
    value='GO !'/>

That was because your DOM will be created on the fly. 那是因为您的DOM将在运行中创建。 So you have to use delegate with jQuery: 所以你必须使用jQuery的委托:

Bind click event on document with selected id: 在具有所选ID的文档上绑定点击事件:

$(document).on('click', '#questlist_item_button_go'+obj.questid, function(){
     // your action here
});

You have to use .on() function to attach an event for dynamically created elements. 您必须使用.on()函数为动态创建的元素附加事件。

EXAMPLE

$(document).on('click', '#DYNAMIC_ELEMENT_ID', function(){
     // your action here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM