最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to return truefalse from nested jquery callback functions - Stack Overflow

programmeradmin2浏览0评论

I am trying to validate elements inside a javascript function which contains two jQuery callback loops. Based on the conditions I want to return true/false from the inner jQuery loop and that should be sent back to the calling method of javascript. If the result of the inner loop is true the loop should stop running.

if(validate(key)){
}
else{
}
function validate(key) {
    $jquery.each(function(){
        $jquery.each(function(){
            if(){
                return true;
            }
            else{
                return false}
        })
    })
}

I am trying to validate elements inside a javascript function which contains two jQuery callback loops. Based on the conditions I want to return true/false from the inner jQuery loop and that should be sent back to the calling method of javascript. If the result of the inner loop is true the loop should stop running.

if(validate(key)){
}
else{
}
function validate(key) {
    $jquery.each(function(){
        $jquery.each(function(){
            if(){
                return true;
            }
            else{
                return false}
        })
    })
}
Share Improve this question edited Aug 9, 2013 at 6:53 Brett DeWoody 62.9k31 gold badges144 silver badges192 bronze badges asked Aug 9, 2013 at 6:27 Paul PhoenixPaul Phoenix 1,4336 gold badges20 silver badges34 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

I think this is what you're looking for, this will stop both loops when the true condition is met

function validate(key) {
    var result = false;
    $jquery.each(function(){
        $jquery.each(function(){
            if(){
                result = true;
                return false;//break inner loop
            }
        });
        if(result)
            return false; //break outer loop if we got true in inner
    });
    return result;
}

Demo fiddle You can open your console and see that the loop stops when the true condition is met

The jQuery docs give a similar example. The docs state 'you can stop the loop from within the callback function by returning false.'

So it sounds like you need to reverse you're terminology and return false when you want the loop to stop.

Let's say we have a bunch of div elements with li elements nested inside. If we wanted to stop the inner loop when it reaches an li with specific content we could do this:

  $( "div").each(function ( index, domEle) {
      $( "li", domEle ).each(function ( index, domEle2) {
        var areWeDone;
        $( domEle2 ).css( "backgroundColor", "yellow" );
              if ( $( domEle2 ).is(":contains('Here')") ) {
                result = true;
                return false;
              } else {
                result = false;
                return true;
              }
      });

      if (result == true) {
          return false;
      } 

  });

Here's the full jsFiddle.

function validate(key) {
    var result;
    $jquery.each(function() {
        $jquery.each(function() {
            if () {
                result = true;
            } else{
                result = false
            }
            return false;
        });
        if (typeof result !== 'undefined') {
            return false;
        }
    });
    return result;
}
    function validate(key) {
    var result;
    $jquery.each(function() {
        $jquery.each(function() {
            if () {
                result = true;

            } else{
                result = false;

            }
            return result;
        });
    });

}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>