jQuery check if any text input has value

后端 未结 5 1155
遇见更好的自我
遇见更好的自我 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条回答
  •  不思量自难忘°
    2020-12-03 10:57

    How about:

    http://jsfiddle.net/lollero/rr2ss/1/

    Another example: http://jsfiddle.net/lollero/rr2ss/12/

    $(function() {
    
        // Check value of each and every input element....
        // Which you can of course change to be more specific, like: $('#myForm input')
        $( "input" ).val(function( i, val ) {
    
            // Return the console log IF value is not empty
            // value=" " still counts as "not empty", because technically it isn't
            // You could of course replace the console.log(); with anything you'd like
            val && console.log('not empty: input-' + (++i) );
    
            // Note that you don't have to return val if you don't  want to, it's just for show in this case.
            return val
    
        });
    
    });
    

提交回复
热议问题