Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

javascript - 在Bootstrap 3选项卡上使用复选框(Using checkbox on Bootstrap 3 tab)

Using Bootstrap 3 and jQuery I have a checkbox like this(使用Bootstrap 3和jQuery我有一个复选框)

var eachrow = "<tr>" + "<td align='center'><input type='checkbox' class='case' name='case[]' value='1' type='checkbox'></td>" + "<td>Test</td>" + "</tr>"; $('#compValues').append(eachrow); $("input:checkbox[name='case[]']").change(function(){ if($(this).is(':checked')){ alert("I am checked"); } else{ alert("I am Unchecked"); } }); Now the check box can be marked (checked or un-checked but the .change() function not doing any thing! no error or something else. Why is this happening?(现在可以选中该复选框(选中或未选中,但是.change()函数不执行任何操作!没有错误或其他错误。为什么会发生这种情况?)   ask by Mona Coder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To attach an event handler to an element that is created dynamically you can use delegated events http://api.jquery.com/on/(要将事件处理程序附加到动态创建的元素上,可以使用委托事件http://api.jquery.com/on/)

example:(例:) $("table").on("change", "input:checkbox[name='case[]']", function() { if($(this).is(':checked')){ alert("I am checked"); }else{ alert("I am Unchecked"); } });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...