Return a value when using jQuery.each()?

后端 未结 3 2017
旧巷少年郎
旧巷少年郎 2020-11-27 18:47

I want to return false and return from function if I find first blank textbox

function validate(){
 $(\'input[type=text]\').each(function(){
   if($(this).va         


        
3条回答
  •  没有蜡笔的小新
    2020-11-27 19:29

    You are jumping out, but from the inner loop, I would instead use a selector for your specific "no value" check, like this:

    function validate(){
      if($('input[type=text][value=""]').length) return false;
    }
    

    Or, set the result as you go inside the loop, and return that result from the outer loop:

    function validate() {
      var valid = true;
      $('input[type=text]').each(function(){
        if($(this).val() == "") //or a more complex check here
          return valid = false;
      });
      return valid;
    }
    

提交回复
热议问题