单击标签不会触发单击事件 [英] Clicking on label doesn't trigger click event
本文介绍了单击标签不会触发单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
<label><input type="checkbox">True?</label>
和
$("label").click(function () {
$(this).toggleClass("active");
});
单击复选框类切换时,单击True?什么都没发生。为什么?
When I click the checkbox class toggles, when I click "True?" nothing happens. Why?
然后,如果我不在标签中包装复选框,一切正常。
Then if I don't wrap checkbox in label everything works fine.
<input type="checkbox" id="c1"><label for="c1">True?</label>
这太奇怪了......如果我把for或者不都没关系。
This is so weird... And it doesn't matter if I put "for" or not.
http://jsfiddle.net/zufs6ueh/ 1 /
这里出了什么问题?
推荐答案
使用起来会更安全:
$(function() {
$('input[type="checkbox"]').bind('change', function (v) {
if($(this).is(':checked')) {
$(this).parent().addClass('active');
} else {
$(this).parent().removeClass('active');
}
});
});
使用更改
而不是点击
允许使用键盘导航表单的人。
Using change
instead of click
allows for people that navigate forms using the keyboard.
这篇关于单击标签不会触发单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文