【发布时间】:2015-05-28 12:17:42
【问题描述】:
我是 jQuery 新手,希望这里有人可以帮助我解决这个问题。
我正在尝试设置一个“全部”复选框来选中/取消选中其所有同级复选框(即那些在同一个 div/父级下的复选框) - 但前提是它们没有特定的类("other")。
我的方法是使运行时间最少,并以一种也可以应用于其他 div 中的类似结构的方式编写它。
我的代码在没有:not() 选择器的情况下工作,但是当我包含它时我无法让它工作。
另外,我不确定我的方法是否是这里最好/最快的方法。
我的 jQuery:
$('.checkAll').click(function(){
if($(this).is(':checked')){
$(this).siblings(":not($('.other'))").prop('checked', true);
}else{
$(this).siblings(":not($('.other'))").prop('checked', false);
}
});
非常感谢您的任何帮助, 迈克
【问题讨论】:
-
来自 w3.org:
Note: During the handling of a click event on an input element with a type attribute that has the value "radio" or "checkbox", some implementations may change the value of this property before the event is being dispatched in the document.所以你应该使用change事件而不是所有现代浏览器都很好地支持click事件 AFAIK。编辑:刚刚检查了 jQuery 源代码,并且像往常一样,这是考虑到(标准化),请参见下面的代码: -
/* For checkbox, fire native event so checked state will be right */ trigger: function() { if ( jQuery.nodeName( this, "input" ) && this.type === "checkbox" && this.click ) { this.click(); return false; } }, -
@A.Wolff:也谢谢你!
标签: jquery checkbox jquery-selectors checked siblings