1

我正在li根据数据库返回的值生成使用 AJAX,并尝试将 jQuery 应用于动态生成的 jQuery li,但它不起作用。任何人都可以帮忙吗?

我的代码是

$('.chkbox1').change(function(){
       if($(this).is(':checked'))
       {
       var grpid=this.getAttribute('id')
       //alert(grpid);
       $.ajax({

   type:"POST",
   url:"<?php print $this->Url ?>index.php/presentation/multimedia/getusers?grpid="+grpid,
   data:"grpid="+grpid,
   success:function(response){
               var data=jQuery.parseJSON(response);
               var str="";
                //alert(data[0]['first_name']);
               for(var i=0;i<data.length;i++)
               {
                  str=str+"<div class='img_block'><ul><li id='"+i+"'><img src='<?php print BASE_URL?>libs/publish_cart/product_img/1.jpg' class='items' height='80' alt='' /><br clear='all' /> <input type='checkbox' id='"+i+"' class='chkbox'/><div class='item_name'>"+data[i]['first_name']+"</div></li></ul> </div>";
                  $(".user").html(str);


               }


}

   });  

chkbox的代码li如下。我想要它,以便在单击复选框时将值添加到购物车中,但没有任何反应。如果我编写静态li代码,那么它可以工作。

var Arrays=new Array();




            $('.chkbox').change(function(){
            if($(this).is(':checked'))
            {

        var thisID    = $(this).attr('id');

        var itemname  = $(this).parent().find('.item_name').html();
        var itemprice = 12;

        if(include(Arrays,thisID))
        {
            var price    = $('#each-'+thisID).children(".shopp-price").find('em').html();
            var quantity = $('#each-'+thisID).children(".shopp-quantity").html();
            quantity = parseInt(quantity)+parseInt(1);

            var total = parseInt(itemprice)*parseInt(quantity);

            $('#each-'+thisID).children(".shopp-price").find('em').html(total);
            $('#each-'+thisID).children(".shopp-quantity").html(quantity);

            var prev_charges = $('.cart-total span').html();
            prev_charges = parseInt(prev_charges)-parseInt(price);

            prev_charges = parseInt(prev_charges)+parseInt(total);
            $('.cart-total span').html(prev_charges);

            $('#total-hidden-charges').val(prev_charges);
        }
        else
        {
            Arrays.push(thisID);

            var prev_charges = $('.cart-total span').html();
            prev_charges = parseInt(prev_charges)+parseInt(itemprice);

            $('.cart-total span').html(prev_charges);
            $('#total-hidden-charges').val(prev_charges);

            var Height = $('#cart_wrapper').height();
            $('#cart_wrapper').css({height:Height+parseInt(45)});

            $('#cart_wrapper .cart-info').append('<div class="shopp" id="each-'+thisID+'"><div class="label">'+itemname+'</div><div class="shopp-price"> $<em>'+'abc'+'</em></div><span class="shopp-quantity">1</span><img src="<?php print BASE_URL?>libs/publish_cart/remove.png" class="remove" /><br class="all" /></div>');

        }
                }




    }); 
4

1 回答 1

2

您应该在动态附加 html 后绑定点击事件。按照以下方式进行:

$('.chkbox').on("change", chkboxChange);

function chkboxChange() {
    //place your code from "$('.chkbox').change(function(){" here;
}

在成功回调中执行 ajax 请求后,请执行以下操作:

success:function(response){
//your logic
    $('.chkbox').off("change").on("change", chkboxChange); //bind event handlers
}
于 2013-03-13T14:19:25.783 回答