簡體   English   中英

使用.each()時如何停止多次附加警報消息

[英]How to stop an alert message appending multiple times when using .each()

我正在使用JQuery .each()來獲取名為“required”的類的所有值。 如果這些輸入中的任何一個為空,我想要彈出一條警告消息。 這一切都很棒。 但是,我對我對警報產生影響的方式感到不滿意。 我想使用delay()然后使用slideUp。 但是,當使用delay()時,我會收到每個傳遞的警報消息。 我只想在使用警報時發出1次警報。 我嘗試使用appendTo()但是沒有用。

我究竟做錯了什么 ?

$(document).ready(function () {

    $("#quotation.btn").mousedown(function () {      // THIS IS THE SUBMIT BUTTON OMN THE FORM

        $(".required").each(function () {      //HERE IS THE EACH

            var required = $(this).val();  // WE GRAB THE VALUES OF THE "REQUIRED FIELDS"

            if (required == "") {                   // IF ANY OF THE REQUIRED FIELDS ARE EMPTY WE EXECUTE THE FOLLOWING:

                var message = '<p class="alert alert-danger" > You have Missed Off One or More Required Fields !</p>'

                $("#collapseOne").collapse('show');
                $("#collapseThree").collapse('hide');

                // message.appendTo($("#alertMessage")).delay(5000).slideUp(500); // THIS DOES NOT WORK !

                $("#alertMessage").append(message).slideUp(10000);  // THIS DOES WORK SO LONG AS I DO NOT USE DELAY() !

                $(this).css({"border": "solid 2px", "color": "#ff6666"})


            }

            else {
                $(this).css({"border": "1px solid #ccc", "background-image": "none"})

            }
        })
    });

});

您只需在條件語句中設置變量:

if (required == "") {
    showAlert = true;
}

然后在each語句之外,您可以檢查是否需要顯示警報:

if (showAlert) {
   $("#alertMessage").append(message).slideUp(10000); 
   showAlert = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM