how to return true/false from nested jquery callback functions

☆樱花仙子☆ 提交于 2019-12-22 09:13:49

问题


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

回答1:


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




回答2:


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.




回答3:


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



回答4:


    function validate(key) {
    var result;
    $jquery.each(function() {
        $jquery.each(function() {
            if () {
                result = true;

            } else{
                result = false;

            }
            return result;
        });
    });

}


来源:https://stackoverflow.com/questions/18141157/how-to-return-true-false-from-nested-jquery-callback-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!