2013-03-13 83 views
1

我使用AJAX生成li,具體取決於數據庫返回的值,並且試圖將jQuery應用於動態生成的li,但它不起作用。誰能幫忙?使用AJAX添加HTML元素

我的代碼是

$('.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); 


       } 


} 

    }); 

li chkbox的代碼如下。我想要它,只需點擊複選框,即可在購物車中添加該值,但沒有任何反應。如果我寫靜態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>'); 

     } 
       } 




    }); 
+1

定義 「不工作」。 – 2013-03-13 14:14:17

+0

javascript控制檯說什麼?任何錯誤? – EmCo 2013-03-13 14:15:37

+0

您必須使用jQuery的'live'函數來偵聽動態創建的元素的事件。 – danijar 2013-03-13 14:17:29

回答

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 
} 
+0

取代很高興聽到這是現代的方式,我比'活着'更喜歡它。 – danijar 2013-03-13 14:23:49