使用jQuery.each()时返回一个值?

时间:2022-09-10 13:37:38

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

如果我找到第一个空白文本框,我想返回false并从函数返回

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

and above code is not working for me :( can anybody help?

以上代码对我不起作用:(任何人都可以帮忙吗?

2 个解决方案

#1


55  

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;
}

#2


14  

You can do it like this:

你可以这样做:

function validate(){
    var rv = true;
    $('input[type=text]').each(function(){
        if($(this).val() == "") {
            rv = false;   // Set flag
            return false; // Stop iterating
        }
    });
    return rv;
}

That assumes you want to return true if you don't find it.

如果您没有找到它,则假定您想要返回true。

You may find that this is one of those sitautions where you don't want to use each at all:

您可能会发现这是一种您不想使用每种措施的注意事项:

function validate(){
    var inputs = $('input[type=text]');
    var index;
    while (index = inputs.length - 1; index >= 0; --index) {
        if (inputs[index].value == "") { // Or $(inputs[index]).val() == "" if you prefer
            return false;
        }
    }
    // (Presumably return something here, though you weren't in your example)
}

#1


55  

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;
}

#2


14  

You can do it like this:

你可以这样做:

function validate(){
    var rv = true;
    $('input[type=text]').each(function(){
        if($(this).val() == "") {
            rv = false;   // Set flag
            return false; // Stop iterating
        }
    });
    return rv;
}

That assumes you want to return true if you don't find it.

如果您没有找到它,则假定您想要返回true。

You may find that this is one of those sitautions where you don't want to use each at all:

您可能会发现这是一种您不想使用每种措施的注意事项:

function validate(){
    var inputs = $('input[type=text]');
    var index;
    while (index = inputs.length - 1; index >= 0; --index) {
        if (inputs[index].value == "") { // Or $(inputs[index]).val() == "" if you prefer
            return false;
        }
    }
    // (Presumably return something here, though you weren't in your example)
}