jQuery可以根据表单元素的值和/或状态选择表单元素的集合吗?
例如,我有一些看起来像这样的代码
jQuery("input[type='checkbox']").each(function(element){
if(this.checked)
{
//do something with the checked checkeboxes
}
});
我想删除内部条件,并以某种方式将其添加到初始选择中。作为选择器字符串的一部分,或者通过链上的一些额外方法调用。
发布于 2009-11-17 07:49:19
对于checked
属性,您可以使用:checked选择器:
var checkedInputs = $('input:checked');
对于其他属性,可以使用attribute filters。
发布于 2009-11-18 01:08:37
此外,对于任意过滤,请使用filter
(正如@CMS所建议的)。它类似于grep
,但专门用于jQuery选择集。
jQuery("input[type='checkbox']")
.filter(function(){ return this.checked; })
.each(function() {
// Do something with "this"
});
https://stackoverflow.com/questions/1745635
复制相似问题