jquery中的"任何"布尔函数

Wil*_*son 12 javascript jquery selector any

有没有简单的方法来检查jquery选择器中的任何元素是否满足条件?例如,要检查表单中的任何文本框是否为空(一种伪,而不是真正的jquery):

$('input.tb').any(val().length == 0);
Run Code Online (Sandbox Code Playgroud)

注意:我知道可以使用辅助方法完成,只是好奇,如果可以在一个语句中.

Mal*_*alk 12

jQuery内置了很多伪选择器:http://api.jquery.com/category/selectors/jquery-selector-extensions/

您还可以使用以下filter()功能构建自己的功能:http://api.jquery.com/filter/

$('input.tb').filter(function() { return this.value.length == 0});
Run Code Online (Sandbox Code Playgroud)


Kyl*_*Mit 11

当前答案以及jQuery自己的过滤功能(包括 .is().has())的问题.filter()在于,一旦满足条件,它们都不会短路。根据您需要评估多少个元素,这可能不是一个很大的性能问题

这是一个简单的扩展方法,它遍历jQuery对象并评估每个元素,因此我们只要符合条件就可以尽早终止。

jQuery.fn.any = function(filter){ 
  for (i=0 ; i<this.length ; i++) {
     if (filter.call(this[i])) return true;
  }
  return false;
};
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

var someEmpty= $(":input").any(function() { 
    return this.value == '';
});
Run Code Online (Sandbox Code Playgroud)

这样会产生更好的性能结果

效果图


如果您采用纯jQuery路由,则与 jQuery 相同,因此可能会使使用更短,更命令式的代码代替。$.is(<sel>) !!$.filter(<sel>).lengthisfilter

jQuery是文档


这是一个实际的例子$.any()

jQuery.fn.any = function(filter){ 
  for (i=0 ; i<this.length ; i++) {
     if (filter.call(this[i])) return true;
  }
  return false;
};

$(function() {

  $(":input").change(function() {
  
    var someEmpty = $(":input").any(function() { 
        return this.value == '';
    });

    console.log("Some Empty:", someEmpty);


  }).eq(0).change(); // fire once on init

})
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" value="">
<input type="text" value="">
<input type="text" value="">
Run Code Online (Sandbox Code Playgroud)

进一步阅读:


Rhy*_*den 7

.is() 用函数参数就是你所追求的.

var anyEmpty = $('input.tb').is(function(index, element) {
    return !element.value;
})
Run Code Online (Sandbox Code Playgroud)

警告:

该功能令人惊讶,因为它不会短路.它将不必要地迭代所有选定的元素,即使回调已经满足.

let i = 0
let result = $('div').is((index, element) => {
  i++;
  return element.tagName === 'DIV';
})
console.log(`count = ${i}, result = ${result}`);
// count = 732, result = true
Run Code Online (Sandbox Code Playgroud)


ade*_*neo 5

if ( $('input.tb[value=""]').length > 0 ) // there are empty elements
Run Code Online (Sandbox Code Playgroud)


Bre*_*ent 5

对于偶然发现这种情况的其他人,我迟到了一年:

Malk的答案将满足您的示例中的精确要求:

$('input.tb').filter(function() { return this.value.length == 0}).length != 0;
Run Code Online (Sandbox Code Playgroud)

执行此操作的稍微更高效的方法(如果早期满足条件,过滤器函数仍将迭代剩余的DOM元素)将编写您自己的帮助器方法(诚然,操作已声明

我知道可以用一个辅助方法完成,只是好奇,如果它可以在一个语句中

,但我来到这个页面希望节省2分钟写我自己的):

;(function($) {
    //iterates over all DOM elements within jQuery object
    //and returns true if any value returned from the supplied function is 'truthy'.
    //if no truthy values are returned, returns false.
    //
    //Function( Integer index, Element element ) 
    $.fn.any = function(fn) {
       var i=0;
       for (;i<this.length;i++) {
          if (fn.call(this[i],i,this[i])) {return true;}
       }
       return false;
    }
)(jQuery);
Run Code Online (Sandbox Code Playgroud)