jQuery check if any text input has value

后端 未结 5 1157
遇见更好的自我
遇见更好的自我 2020-12-03 10:31

I want to ask if there\'s a better way in jQuery to select multiple text input then check if any of them has a value. Here\'s my code:

if ($(\"#reference\")         


        
5条回答
  •  萌比男神i
    2020-12-03 11:13

    The problem with getting the length property on filter() is that jQuery will evaluate every single element in the collection, just to populate a count when all we care about is whether the value is greater than zero.

    None of the current answers and even jQuery's own .is(), .has(), and .filter() make use of short circuiting as soon as the criteria is met.

    You can define a simple extension method called .any() like this:

    jQuery.fn.any = function(filter){ 
        for (i=0 ; i

    And then pass in a filtering function like this:

    var someInputsEmpty = $("#reference,#pin,#fName,#mName,#datepicker").any(function() { 
        return this.value == '';
    });
    

    jQuery.fn.any = function(filter){ 
    	for (i=0 ; i
    
    
    
    
    

    Further Reading:

    • Jquery check form to see if any inputs has value
    • Is there an "exists" function for jQuery?
    • Jquery check form to see if any inputs has value

提交回复
热议问题