2013-03-18 58 views
15

我需要一個jQuery濾波器/圖/每種類型的功能,以檢查是否所有元素滿足條件:檢查是否所有元素滿足條件

function areAllValid(inputs){ 
    return $.someFunction(inputs, function(input) { return input.length > 0; }); 
} 

如果所有輸入具有長度> 0 someFunction應返回true。在jQuery中是否這樣?

+1

我不要以爲jQuery有這個。 Underscore.js有'$ .every()'。使用'$ .filter'在jQuery中實現應該很簡單。 – Barmar 2013-03-18 06:20:20

+0

注意,如果你可以找到一種方法來描述你的條件作爲選擇器,那麼你可以檢查集合中'inputs.remove('[value = ...]')的條目數。length == 0' Something沿着一個「值不空選擇器」的行:http://stackoverflow.com/questions/10641258/jquery-select-data-attributes-that-arent-empty – AaronLS 2013-03-18 06:41:51

+0

@Barmar,every()正是我需要和巧合的是已經強調已經作爲一個依賴:)。謝謝 – parliament 2013-03-18 20:21:02

回答

8

答案是肯定的,它有一個方法grep可以滿足您的要求。例如:

inputs= jQuery.grep(inputs, function(input){ 
return input.length>0; 
}); 
if(inputs.length>0) return true; 
return false; 

我沒有測試它,或許它有微小的問題,但應該差不多是這樣。

+0

從文檔看起來像這樣會工作,但我沒有測試。我最終用Underscore.every()去做,因爲我已經將它作爲一個依賴關係,它完全符合填充,返回一個布爾值。 – parliament 2013-03-18 20:20:14

+6

這樣做的問題是,如果您的數千個元素中的第二個元素不符合要求,它仍然會檢查以下所有元素,而不是打破循環並返回「false」。但它的工作。 – bfontaine 2014-09-18 09:01:42

3

這將遍歷每個項目,並將條件與以前的結果進行比較,以便只有在條件對所有項目都爲真時纔會返回true。你當然可以使用回調函數替換條件,並且執行inputs.each(而不是$('input'),但是您可能需要稍微調整代碼,具體取決於輸入是否是jquery對象。

var all = true; 
    $('input').each(function(index, value) { 
    all = all & ($(value).val().length > 0); 
    }); 
return all; 
0

不完全是,但它很容易地創建一個:

$.eachCondition = function (obj, conditionFunction){ 
     var trueCount=0; 
     var falseCount=0; 

     $.each(obj, function (i,v) { 
      if (conditionFunction.call(this,i,v)) { 
      trueCount++; 
      } 
      else { 
      falseCount++; 
      } 
      }); 
      if (falseCount===0) { 
      return true; 
      } 
      if (trueCount===0) { 
      return false; 
      } 
      return undefined; 
     }; 
     $.fn.eachCondition = function (conditionFunction) { 
     return $.eachCondition(this, conditionFunction); 
     }; 

這裏正在測試:http://jsbin.com/iwanof/2/

0

我通常使用下面的表格來獲得比賽在JQuery中

$.map($("[some selector]"), function(e) { 
    return ([some matching mechanism]); 
}).indexOf(false) == -1; 

在您的具體情況下,它看起來像這樣:

$.map(inputs, function(e) { 
 
    return (e.length > 0); 
 
}).indexOf(false) == -1;

希望這可以節省一些時間。

+0

我想你也可以使用'inputs.map(function ...'。 – 2018-01-23 21:23:55

2

你不需要使用jQuery來做到這一點。您可以使用IE9 +支持的Array.prototype.every在原生JavaScript中執行此操作。

function areAllValid(inputs){ 
    return inputs.every(function(input) { return input.length > 0; }); 
} 

如果您正在使用的EcmaScript 2015年,您可以進一步整理一下:

var areAllValid = inputs => inputs.every(input => input.length > 0); 
+0

這個值得被提高 – 2017-06-05 06:13:01

0

存在由現有的解答未解決一些小的優化問題,所以我把我的帽子環與我認爲是最具可讀性/性能的代碼。

添加一個jQuery的擴展方法,這樣將短路:

/** 
* Determines whether all elements of a sequence satisfy a condition. 
* @@param {function} predicate - A function to test each element for a condition. 
* @@return {boolean} true if every element of the source sequence passes the test in the specified predicate 
*/ 
$.fn.all = function (predicate) { 
    $(this).each(function (i, el) { 
     if (!predicate.call(this, i, el)) return false; 
    }); 
    // we made it this far, we're good. 
    return true; 
}; 

然後調用它像這樣:

var allValid = $("form :input").all(function() { 
        return $(this).valid(); 
       }); 
0

快速小的jQuery:

$(document).ready(function() { 
    $('form input').keyUp(function() { 
    var allValid = true; 
    $.each($('form input'), function(input) { 
     allValid = input.val().length > 0 
    } 
    if (allValid) 
     ... 
    else 
     ... 
    }) 
}); 
0
function areAllValid(inputs){ 
    return Array.prototype.every.call(inputs, function(input) { return input.length > 0; }); 
} 
相關問題