既存の回答では対処されていないいくつかの小さな最適化の問題があるため、最も読みやすくパフォーマンスの高いコードであると私が信じているものに敬意を表したいと思います。
短絡する次のような 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();
});