2014-01-27 40 views
0

我有一个jquery-ajax函数,它创建了一个复选框列表。jquery复选框使用JSON创建的点击功能不起作用

success: function(msg){ 

    jQuery.each(msg.gal_sublocations, function(index, gal_sublocation) { 
     html += '<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "'+gal_sublocation.GalSublocation.id+'" class="chkbx" id="sublocation_'+gal_sublocation.GalSublocation.id+'" />'+gal_sublocation.GalSublocation.name; 

     $('#load_sublocations').html(html); 
     alert(html); 
     $('#sublocation_'+gal_sublocation.GalSublocation.id).click(function(){ 
      alert('Checked !'); 
     }); 

    }); 

} 

alert(html)产生最终的警觉

<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "99" class="chkbx" id="sublocation_99" />Beltola 
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "101" class="chkbx" id="sublocation_101" />Dispur 
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "102" class="chkbx" id="sublocation_102" />Paltan Bazar 
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "100" class="chkbx" id="sublocation_100" />Patarkuchi 

但是当我点击复选框,它没有任何警告!怎么了 ?

回答

0

您需要使用jquery On(),因为您的代码在加载后附加。

这样使用;

$(".class").on('click', function(){ 
// ajax operation 
}} 
+0

喜Mehmet..it还没有工作!相同。只有最后一个复选框工作! – Nitish

0

在创建您的输入字符串,最好创建一个节点就在那里:

var html = $("<input />").attr({ 
    "type" : "checkbox", 
    "name" : data[GalStore][gal_sublocation_id][], 
    "id" : 'sublocation_' + gal_sublocation.GalSublocation.id, 
    "class" : "chkbx" 
}) 
.val(gal_sublocation.GalSublocation.id) 
.click(function(){ 
     alert('Checked !'); 
}); 

$('#load_sublocations').append(html); 
+0

嗨阿希什,它不工作!它甚至没有填充复选框:( – Nitish

+0

真的,在这里做一个检查:http://jsfiddle.net/8wtJG/1/ 事情评论,因为这些数据来自你的ajax,在我的例子中,不可用。 –