2016-11-23 178 views
2

我想了解。选择ul内的所有复选框

为什么这是行不通的:

$(this).closest("li").find("ul:checkbox").prop("checked", true); 

但这决?

$(this).closest("li").find("ul").find(":checkbox").prop("checked", true); 

感谢

+2

道具使用'.prop()'方法(没有双关),而不是'.attr()'。我无法告诉你在这种情况下我看到有多少人使用错误的方法... – War10ck

回答

7

在你的第一个例子中的问题,是因为ul:checkbox。这查找<ul>也是复选框的元素,这是不可能的。在这些选择器之间需要一个空格,或者像第二个示例中那样使用find()

的这些任一将工作:

$(this).closest("li").find("ul :checkbox").prop("checked", true); 
// or 
$(this).closest("li").find("ul").find(":checkbox").prop("checked", true);