将事件添加到 jquery mobile 中动态创建的复选框 [英] Adding events to dynamically created checkbox in jquery mobile
本文介绍了将事件添加到 jquery mobile 中动态创建的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在页面中动态创建了复选框.我想为所有复选框添加点击事件动态创建的.
这是我的代码.
<fieldset data-role="controlgroup" id="br"></fieldset>
我动态创建了复选框并附加到字段集.
$.getJSON("http://localhost/WebSite/",功能(数据){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("介绍");});$('#br').append(branchOp).trigger("创建");});
我使用 on()、live()、deligate() 方法在复选框上添加事件处理程序.
$('#br').delegate('click','input:checkbox', function(event) {警报('选择');});$('#br').on('click','input:checkbox', function(event) {警报('选择');});
没有什么对我有用...
解决方案
使用复选框/单选按钮,使用 change
事件.
$(document).on('change', '[type=checkbox]', function() {//代码在这里});
此外,您可以使用 click
但在包装复选框的 div 上.
$(document).on('click', 'div.ui-checkbox', function() {//代码在这里});
I created my check box dynamically in my page. i want to add click event for all checkboxes that dynamically created.
here is my code.
<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" );
});
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...
解决方案
With checkbox / radio buttons, use change
event.
$(document).on('change', '[type=checkbox]', function() {
// code here
});
Also, you can use click
but on the div wrapping the checkbox.
$(document).on('click', 'div.ui-checkbox', function() {
// code here
});
这篇关于将事件添加到 jquery mobile 中动态创建的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文