简体   繁体   English

删除最后一个 div 而不是使用 jQuery 超时将它们全部删除

[英]Remove last div instead of removing them all using jQuery timeout

I'm using AJAX to submit the form without reloading.我正在使用 AJAX 提交表单而无需重新加载。 I'm using it for my register form.我将它用于我的注册表单。 After I catch my error/success message I append it to my div and remove it after 5 seconds using timeout function.捕获错误/成功消息后,我将其附加到我的 div 中,并在 5 秒后使用超时功能将其删除。

Now if the user clicks on the button and gets error, and then again clicks the button (before 5 seconds pass) the second error message div will appear with the same error but it will be removed at the same time the first one is removed.现在,如果用户单击按钮并出现错误,然后再次单击按钮(在 5 秒过去之前),第二个错误消息 div 将出现相同的错误,但它会在第一个被删除的同时被删除。 So what I'm trying to achieve is to set my error/success messages to have individual timeouts.所以我想要实现的是将我的错误/成功消息设置为具有单独的超时。 So first error appears, then after 2 seconds I press the button again and get second error, now the first error will be removed after 5 seconds, and the second will stay untill its 5 seconds pass.所以第一个错误出现,然后在 2 秒后我再次按下按钮并得到第二个错误,现在第一个错误将在 5 秒后删除,第二个将保持直到 5 秒过去。

So I have a div in my HTML code with the id "msg-response", where my error/success messages appear.所以我的 HTML 代码中有一个 ID 为“msg-response”的 div,其中显示了我的错误/成功消息。 And here is how I call them:这是我如何称呼它们的:

$("#registerform").submit(function(event) {
    var ajaxRequest;
    event.preventDefault();
    $("#result").html('');
    var values = $(this).serialize();
       ajaxRequest = $.ajax({
            url: "include/auth/register.php",
            type: "post",
            data: values
        });
    ajaxRequest.done(function (response){
         if ($.trim(response) == 'error') {
            $("#msg-response").prepend("<div class='error-msg'>Email format isn't valid. Try again.</div>").hide().fadeIn("slow");
            setTimeout(function() {
                $('.error-msg').fadeOutAndRemove('slow');
            }, 5000);
         }
    });

    ajaxRequest.fail(function (){
        alert("error");
    });

});

So how can I add individual timeouts to each div appeared?那么如何为每个出现的 div 添加单独的超时? And not one timeout for all divs with class .error-msg .而不是所有.error-msg类的 div 超时。

Thanks in advance.提前致谢。

You can store your .error-msg inside a var and then remove it by var reference:您可以将.error-msg存储在var ,然后通过var引用将其删除:

var error = $("<div class='error-msg'>Email format isn't valid. Try again.</div>"); 
error.fadeOutAndRemove('slow');

Final code:最终代码:

     if ($.trim(response) == 'error') {
        var error = $("<div class='error-msg'>Email format isn't valid. Try again.</div>");

        $("#msg-response").prepend(error).hide().fadeIn("slow");
        setTimeout(function() {
            error.fadeOutAndRemove('slow');
        }, 5000);
     }

Intresting, I got it working by making my error-msg div to be an ID instead of CLASS, so like this:有趣的是,我通过让我的 error-msg div 成为一个 ID 而不是 CLASS 来让它工作,就像这样:

$("#registerform").submit(function(event) {
    var ajaxRequest;
    event.preventDefault();
    $("#result").html('');
    var values = $(this).serialize();
       ajaxRequest = $.ajax({
            url: "include/auth/register.php",
            type: "post",
            data: values
        });
    ajaxRequest.done(function (response){
         if ($.trim(response) == 'error') {
            $("#msg-response").prepend("<div id='error-msg'>Email format isn't valid. Try again.</div>").hide().fadeIn("slow"); // Changed line
            setTimeout(function() {
                $('#error-msg').fadeOutAndRemove('slow'); // Changed line
            }, 5000);
         }
    });

    ajaxRequest.fail(function (){
        alert("error");
    });

});

I'm confused a little, so can atleast someone write me explanation why its good now that I'm using ID and not while using classes?我有点困惑,所以至少有人可以写给我解释为什么现在我使用 ID 而不是在使用类时很好? And is this the right approach?这是正确的方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM