显示后重新加载Ajax表单

时间:2018-08-27 00:31:50

标签: ajax

我有一个使用Ajax的联系表。提交数据后(使用PHP脚本),该表单将显示一条已发送的消息。但是,显示不会在延迟后消失。它只有一个“ x”供用户关闭。我想我找到了一种淡出的方式,但是需要重置页面(窗体?)才能再次使用它。这是消息的脚本。

$(function () {

  // init the validator
  // validator files are included in the download package
  // otherwise download from http://1000hz.github.io/bootstrap-validator

  $('#contact-form').validator();

  // when the form is submitted
  $('#contact-form').on('submit', function (e) {

    // if the validator does not prevent form submit
    if (!e.isDefaultPrevented()) {
        var url = "/contact.php";

        // POST values in the background the the script URL
        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {
              // data = JSON object that contact.php returns

              // we recieve the type of the message: success x danger and apply it to the
              var messageAlert = 'alert-' + data.type;
              var messageText = data.message;

              // let's compose Bootstrap alert box HTML
              var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';

              // If we have messageAlert and messageText
              if (messageAlert && messageText) {
                // inject the alert to .messages div in our form
                $('#contact-form').find('.messages').html(alertBox);
                $('.messages').delay(5000).fadeOut();  // *********** ADDED BY ME ***************
                // empty the form
                $('#contact-form')[0].reset();
              }
            }
        });
        return false;
      }
  })
});

我知道$('#contact-form')[0].reset();应该重置它,但不允许它再次运行。

我需要做些什么来重置可重用的表单,而无需重新加载页面(这会破坏Ajax的使用)?

0 个答案:

没有答案
相关问题