Jquery on event not working for Dynamic elements

社会主义新天地 提交于 2019-12-12 04:42:29

问题


$(".spanCont:first .collection_shop").on("click",function(){
            var current_item = $(this);
            $.ajax({
                url: "ajax/abc.php",
                type: "POST",
                dataType: 'html',
                data: {collection_id: current_item.attr("value")},
                beforeSend: function(xhr) {
                    current_item.replaceWith("<div id='temp_div'></div>");
                }
            }).done(function(data){
                $(".spanCont:first .span-2, .spanCont:first input").remove();
                $("#temp_div").replaceWith(data);
            });
        });

This code should work for all static and dynamic click of elements with class .collection_shop but its only working for static elements.


回答1:


you should use event delegation for that

$(document).on("click",".spanCont:first .collection_shop",function(){
   //some operation
});

It helps you to attach handlers for dynamic elements




回答2:


Use event delegation

$(document).on("click",".spanCont:first .collection_shop",function(){
//code
});



回答3:


use .on()

Use Event Delegation

Syntax

$( elements ).on( events, selector, data, handler );

like this

$(document).on("click",".spanCont:first .collection_shop",function(){
   // code here
});

or

$('parentElementPresesntAtDOMready').on('click','.spanCont:first .collection_shop',function(){
   // code here
});



回答4:


You need other (delgation) version of on() for dynamic elements. Delegate event to static parent of the dynamically elements or you can use document / body etc.

$(document).on("click", ".spanCont:first .collection_shop", function(){
    var current_item = $(this);
    $.ajax({
        url: "ajax/abc.php",
        type: "POST",
        dataType: 'html',
        data: {collection_id: current_item.attr("value")},
        beforeSend: function(xhr) {
            current_item.replaceWith("<div id='temp_div'></div>");
        }
    }).done(function(data){
        $(".spanCont:first .span-2, .spanCont:first input").remove();
        $("#temp_div").replaceWith(data);
    });
});

You have

$(".spanCont:first .collection_shop").on("click",function(){

You need, for event delegation

$("static-parent-selector").on("click", .spanCont:first .collection_shop, function(){

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers, jQuery Docs




回答5:


Another approach, a little kinder to your ui than setting event delegation from the root 'document'

Split the AJAX from the listener to it's own function and create a listener function that 're listens' after the DOM has been updated by the code in the ajax call.

It's good to separate anyway ( say in the future you want to trigger the ajax request from something else)

function ajaxCall() {

  /* ... do the ajax call and return data   */
 .....done(function() { 

  /* update the DOM et al */

  /* reset the listener */
  listen();

 });
}

function listen() {
 $(".spanCont:first .collection_shop").off("click").on("click",function(){
   ajaxCall();
 });
}
/* start */
listen();

like - http://jsbin.com/foruyapi/1/edit



来源:https://stackoverflow.com/questions/22377865/jquery-on-event-not-working-for-dynamic-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!