【问题标题】:Stop parent function or method in JQuery在 JQuery 中停止父函数或方法
【发布时间】:2012-04-17 22:11:28
【问题描述】:

我有以下代码:

$("#add-button").click( function(add_color) {

    $.each( $("input[name='color[]']"), function () {

        if( color_selected == $(this).val() )
        {
            alert( "Color already choosen" );
            return false;
        }

    });

    alert("Here is the end of add-button");

});

如果调用 return false,我需要停止代码,但 JQuery 只停止 $.each 方法。

我使用了“Event.stop(add_color)”而不是“return false”,它有效,但我不知道这是否正确。

有人可以帮帮我吗?

【问题讨论】:

    标签: jquery function methods


    【解决方案1】:

    你总是可以这样做的:

    $("#add-button").click( function(add_color) {
        var doIStop = false;
        $.each( $("input[name='color[]']"), function () {
    
            if( color_selected == $(this).val() )
            {
                alert( "Color already choosen" );
                doIStop = true;
            }
    
        });
        if(doIStop) {
            return false;
        }
        alert("Here is the end of add-button");
    });
    

    不过,Event.stop 通常没问题。

    【讨论】:

    • 我使用了带有布尔标志的解决方案,它工作正常。非常感谢您的帮助!
    • 没问题,很高兴能帮上忙。如果您将此答案标记为解决方案,我将不胜感激。
    猜你喜欢
    • 2011-08-14
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多