简体   繁体   English

在Ajax成功时显示消息

[英]Displaying message on Ajax success

I have the following piece of javascript which passes data to my database and then returns a success message. 我有以下一段javascript,它将数据传递到我的数据库,然后返回成功消息。

At the moment on success the button fades out and hides perfectly as it should, but the success message doesn't display. 在成功的那一刻,该按钮会淡出并完美隐藏,但不会显示成功消息。

$(document).ready(function () {
    $('form.user_status').submit(function () {
        var userna = $(this).find("[name='userna']").val();
        var joborder_id = $(this).find("[name='joborder_id']").val();
        var user_email = $(this).find("[name='user_email']").val();
        var app_status = $(this).find("[name='app_status']").val();
        // ...
        $.ajax({
            type: "POST",
            url: "user_status.php",
            data: {
                userna: userna,
                joborder_id: joborder_id,
                user_email: user_email,
                app_status: app_status
            },
            success: function () {
                $('form.user_status').hide(function () {
                    $('div.success').fadeOut();
                });
                success = 'Status changed';
            }
        });
        return false;
    });
});

the html where the success message should be dsplayed 应该在其中显示成功消息的html

<div class="success" style="display: none;"></div>

You don't actually do anything with the success string. 您实际上对成功字符串不做任何事情。

Why not go: 为什么不去:

success: function(data){
    $('form.user_status').hide();
    $('div.success').html('Status changed').delay(1000).fadeOut();
}

UNLESS Your success div is inside your form. 除非您的成功div在表格中。 If so then your div will disappear immediately with the form. 如果是这样,那么您的div将立即与表格一起消失。 Which is a bit silly. 这有点愚蠢。 Note I've added data - you need to pass what you've returned (or how will you use the data if you wanted to?) 请注意,我已经添加了数据-您需要传递返回的信息(或者如果需要的话,如何使用数据?)

Display none means by default obviously your div is invisible. 在默认情况下,不显示任何内容意味着您的div是不可见的。 fadeout is just going to keep it invisible surely? 淡出肯定会使其保持不可见?

Edit: So I have removed a bunch of stuff. 编辑:所以我已经删除了一堆东西。 If you are just looking to change what is written in the div then make it disappear. 如果您只是想更改div中写的内容,请使其消失。 You insert html then fadeout and it'll take care of it for you. 您插入html,然后淡出,它会帮您处理。

JSFiddle 的jsfiddle

Delay is there to give the user a second to read it. 延迟使用户有时间阅读它。

In jQuery, ajax method has success method called when ajax response succeed. 在jQuery中,ajax方法有一个成功方法,当ajax响应成功时调用该方法。 As ajax is asynchronous, after executing $.ajax, the compilation cursor moves to next line. 由于ajax是异步的,因此在执行$ .ajax之后,编译光标将移至下一行。 Whatever code you write in success method will gets executed immediately after ajax success. 无论您用成功方法编写的任何代码都将在ajax成功后立即执行。 you need to change DOM in success method, not a variable. 您需要以成功方法(而不是变量)更改DOM。

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

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