如果遇到某个条件,如何从“elems”数组中取消设置当前元素?
var elems = $('input, select, textarea', this);
elems.each(function(){
if($(this).attr('name') == 'something') {
// unset `this` from elems ??
}
});
我在它上面做了一个console.log,它似乎没有键......
答案 0 :(得分:6)
您应该在初始选择器中执行此操作:
var elems = $('input, select, textarea', this).not('[name="something"]');
答案 1 :(得分:5)
您可以使用filter()
函数,该函数构造从谓词函数返回true
的新jQuery对象。
var elems = $('input, select, textarea', this);
elems = elems.filter(function(){
if($(this).attr('name') == 'something') {
return false;
}
return true;
});
这当然可以成为;
var elems = $('input, select, textarea', this).filter(function(){
if($(this).attr('name') == 'something') {
return false;
}
return true;
});
有关详细信息,请参阅filter()
docs。
答案 2 :(得分:3)
elems.filter(function(){
return $(this).attr('name') !== 'something';
});
这会将元素集合减少到名称属性不是“某事”的元素(换句话说:如果名称属性是“某事”,则该元素将被过滤掉)< / p>