成功提交ajax后如何清除表单

时间:2016-06-25 18:37:24

标签: jquery ajax

我想在提交成功后让联系表格消失,但我不知道如何去做。任何援助将不胜感激。

jQuery(document).ready(function($) {
$("#ajax-contact-form").submit(function() {

        var str = $(this).serialize();

        $.ajax({
            type: "POST",
            url: "contactfinal.php",
            data: str,
            beforeSend: function() {
                $("#submit").hide();
                 $("#loading").show();
                  },
            success: function(response) {
               $('#error').html(response);
                $("#submit").show();
                $("#loading").hide();

            }
        });
        return false;
    });
});

1 个答案:

答案 0 :(得分:2)

假设您执行服务器端验证,您应检查响应是否为空,然后隐藏表单:

success: function(response) {
    $('#error').html(response);
    if (!response || response.length == 0)
        $("#ajax-contact-form").hide();

这是理想的方式,但是根据您的源代码,您需要一个" hack":

if (!response || response.length == 0 || response.indexOf("Your message was sent") >= 0)
        $("#ajax-contact-form").hide();
相关问题