切换标记复选框的事件
问题描述:
我有两个复选框。每个人都有一个子窗体,当复选框未被标记时被禁用。我想标记每个复选框的子窗体启用。切换标记复选框的事件
我试试这个代码,但这个工程即使复选框未被选中:
$("#form1").click(function(){
if(":checked",this) { ..... }
});
$("#form2").click(function(){
if(":checked",this) { ..... }
});
你会帮我吗?
答
您可以使用jQuery的is
方法:
if($(this).is(":checked")) {
//It's checked!
}
什么你现在将始终为true,因为this
一直在不断地truthy(不undefined
或null
为例)。
请注意,您需要将this
传递给jQuery,因为它将引用点击的DOM元素本身,而不是jQuery包装的元素。
答
是#form1和#form2复选框?那么你可以做:
$("#form1").change(function(){
if(this.checked) {
.....
}
});
$("#form2").change(function(){
if(this.checked) {
...
}
});
http://jsfiddle.net/9pgmN/1/各种冗余的对象建立和选择过滤后(只是一个演示)
jQuery的":checked"
归结为
checked: function(elem) {
return elem.checked === true;
}