0

複数の関数を介して戻ることは可能ですか?

$.each ループを含む jQuery on click 関数があります。$.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 ループを中断するだけでなく、オン クリック機能から完全に戻りたいと考えています。

  1. これは可能ですか?
  2. もしそうなら、どうすればこれを達成できますか?
4

2 に答える 2

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
于 2013-09-25T14:26:43.270 に答える
0

のドキュメントをチェックしてくださいjQuery.each():

コールバック関数が false を返すようにすることで、特定の反復で $.each() ループを中断できます。false 以外を返すことは、for ループの continue ステートメントと同じです。次の繰り返しにすぐにスキップします。

基本的に、return false;の代わりに使用しreturn;ます。

于 2013-09-25T14:12:17.493 に答える