在使用jQuery返回表单数据之前,不要添加加载类

Hot to add a loading class until the form data are returned using jQuery?

本文关键字:添加 加载 jQuery 返回 数据 表单      更新时间:2023-09-26

在提交时,我想在#results中添加一个.loading类,直到数据返回,所以类被删除。

代码如何更改?

$(function(){
    $("#myform").submit(function(event){
        event.preventDefault();

        $.post('mail.php', $("#myform").serialize(), function(data) {
            $('#results').hide().html(data).fadeIn('slow').delay(2000).hide(1);
        });
    });
});

如果我没理解错的话:

$(function(){
    $("#myform").submit(function(event){
        event.preventDefault();
        // Add the class here:
        $("#results")
            .show()
            .addClass("loading");
        $.post('mail.php', $("#myform").serialize(), function(data) {
            $('#results')
                .hide()
                .html(data)
                .fadeIn('slow')
                .delay(2000)
                .hide(1)
                .removeClass("loading"); // Remove the class here.
        });
    });
});