【问题标题】:Return through all functions通过所有函数返回
【发布时间】:2013-09-25 14:08:45
【问题描述】:

是否可以通过多个函数返回?

我有一个 jQuery on click 函数,其中有一个 $.each 循环。在 $.each 循环中,我测试各种条件,如果不满足,则显示警报消息,然后返回。这是我的代码的精简版:

$(document).on('click', '.add-to-basket, #add-to-basket', function(e) {
    var data = {
        id: $(this).data('id'),
        quantity: 1
    };
    if($('#quant').length > 0) {
        data.quantity = $('#quant').val();
    }

    var i = 0;
    var j = 0;
    if($('.product-option').length > 0) {
        $('.product-option').each(function(index, element) {
            if($(this).is('select')) {
                //check to see if this is a required select, and return if a selection has not been made.
                if($(this).data("force") == 1 && $(this).val() == 0) {
                    AlertDialogue($(this).data("title") + " requires a selection before you can add this product to your basket.", "Required Option");
                    return;
                }
                data.opts[i++] = $(this).val();
            } else if($(this).is('input[type="checkbox"]:checked')) {
                data.opts[i++] = $(this).val();
            //check to see if this is a required group of checkboxes, and if so at least one has been checked. If not return.
            } else if($(this).is('input[type="checkbox"]')) {
                if($(this).data("force") == 1 && $('input[name="' + $(this).prop("name") + '"]:checked').length == 0) {
                    AlertDialogue($(this).data("title") + " requires at least one option to be checked before you can add this product to your basket.", "Required Option");
                    return;
                }
            } else if($(this).is('input[type="radio"]:checked')) {
                data.opts[i++] = $(this).val();
            } else if($(this).is('textarea')) {
                //Check to see if this is a required textarea, and if so make sure there is some text in it.
                if($(this).data("force") == 1 && $.trim($(this).val()).length == 0) {
                    AlertDialogue($(this).data("title") + " requires text before you can add this product to your basket.", "Required Option");
                    return;
                }
                if($(this).val().length > 0) {
                    data.text[j].id = $(this).data("id");
                    data.text[j++].val = $(this).val();
                }
            }
        });
    }
    //submit product to the cart
});

然而,返回只会打破 $.each 循环的那个循环,并开始下一个循环。我不仅想打破 $.each 循环,还想完全从 on click 函数返回。

  1. 这可能吗?
  2. 如果是这样,我该如何实现?

【问题讨论】:

    标签: javascript jquery function


    【解决方案1】:

    要退出$.each,你应该return false

    要退出事件处理函数,你应该使用return

    根据您的要求,您可以执行以下操作,

    var break = false;
    $('.product-option').each(function(index, element) {
        // rest of code
    
        if(condition) {
            break = true;
            return false;           //  this will break out of each loop
        }
    });
    if(break) {
        return;                    // return from event handler  if break == true;
    }
    // rest of code
    

    【讨论】:

      【解决方案2】:

      查看jQuery.each() 的文档:

      我们可以通过让回调函数返回 false 来在特定的迭代中打破 $.each() 循环。返回非false是一样的 作为 for 循环中的 continue 语句;它会立即跳到 下一次迭代。

      基本上,使用return false; 而不仅仅是return;

      【讨论】:

      • 谢谢你,这至少让我成功了一半。这让我永久脱离了 $.each,但它并没有让我脱离原来的点击功能。
      • 为点击处理程序放置一个单独的返回语句。我真的不能给你任何代码,因为我不知道你到底想返回什么。如果您只想离开点击处理程序,请将return false; 放在最后一个括号之前。 注意:这将停止默认的事件动作和事件传播。
      猜你喜欢
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2020-05-26
      • 1970-01-01
      相关资源
      最近更新 更多