使用jQuery.each()时返回值? [英] Return a value when using jQuery.each()?

查看:69
本文介绍了使用jQuery.each()时返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

及以上代码对我不起作用:( 有人可以帮忙吗?

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

推荐答案

您正在退出,但是从 inner 循环中,我将使用选择器来进行特定的无值"检查,像这样:

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

这篇关于使用jQuery.each()时返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆