在jquery mobile中将事件添加到动态创建的复选框

问题描述:

我在页面中动态创建了我的复选框.我想为所有复选框添加点击事件 动态创建的.

I created my check box dynamically in my page. i want to add click event for all checkboxes that dynamically created.

这是我的代码.

<div data-role="fieldcontain">
         <fieldset data-role="controlgroup" id="br">

        </fieldset> 
</div>

我动态创建了复选框并附加到字段集.

i dynamically created checkboxes and append to fieldset.

$.getJSON("http://localhost/WebSite/",
    function(data){ 
    var branchOp="";
      $.each(data.branch, function(i,item){   
         branchOp += '<input type="checkbox" name="branch" id="'+item.branchCode+'" value="'+item.branchCode+'" class="branch"><label for="'+item.branchCode+'">'+item.branchName+'</label>'; 
         $(item.branchCode).addClass("intro");     
      });
      $('#br').append(branchOp).trigger( "create" );  
      });

我使用on(),live(),deligate()方法在复选框上添加事件处理程序.

i use on(), live(), deligate() method to add event handlers on check boxes.

$('#br').delegate('click','input:checkbox', function(event) {
  alert('selected');
 }); 



$('#br').on('click','input:checkbox', function(event) {
alert('selected');
});

没有什么对我有用...

nothing is working for me...

使用复选框/单选按钮,使用change事件.

With checkbox / radio buttons, use change event.

演示

Demo

$(document).on('change', '[type=checkbox]', function() {
// code here
}); 

此外,您可以使用click,但在div上会选中该复选框.

Also, you can use click but on the div wrapping the checkbox.

$(document).on('click', 'div.ui-checkbox', function() {
  // code here
});