我在页面中动态创建了复选框。我想为所有复选框添加点击事件 动态创建的。
这是我的代码。
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" id="br">
</fieldset>
</div>
我动态创建了复选框并附加到字段集。
$.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() 方法在复选框上添加事件处理程序。
$('#br').delegate('click','input:checkbox', function(event) {
alert('selected');
});
$('#br').on('click','input:checkbox', function(event) {
alert('selected');
});
没有什么对我有用......
最佳答案
对于复选框/单选按钮,使用 change
事件。
$(document).on('change', '[type=checkbox]', function() {
// code here
});
此外,您还可以使用 click
,但要在包含复选框的 div 上单击。
$(document).on('click', 'div.ui-checkbox', function() {
// code here
});
关于jquery - 将事件添加到 jquery mobile 中动态创建的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378537/