提交后刷新表单页面上的thankyou消息

时间:2016-09-03 05:06:36

标签: javascript jquery forms

我在页面中有一个简单的表单。表单提交到同一页面并在那里显示感谢信息。但问题是我想在几秒钟后删除感谢信息或刷新页面。有没有解决方案。

这是脚本:

<script>
    /* ==============================================
    Ajax Submiting For Email Subscriber Form.
    =====================================================================*/ 
    $("#subscriber_form").submit(function(e) {
      $('#show_subscriber_msg').html('<div class=gen>Submiting..</div>');
      var subscriberemail = $('#subscriberemail').val();
      var formURL = $(this).attr("action");
      var data = {
        subscriberemail: subscriberemail
      }
      $.ajax({
        url: formURL,
        type: "POST",
        data: data,
        success: function(res) {
          if (res == '1') {
            $('#show_subscriber_msg').html('<div class=gen>Thanks for your interest in Water Lily Pond. We are going to get in touch with you very soon. </div>');
          }
      if (res == '5') {
            $('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
          }
        }
      });
      e.preventDefault();
      //STOP default action
    });
    </script>

6 个答案:

答案 0 :(得分:0)

在成功回调中,为执行所需行为的函数设置超时。

success: function(res) {
  if (res == '1') {
    $('#show_subscriber_msg').html('<div class=gen>Thanks for your interest in Water Lily Pond. We are going to get in touch with you very soon. </div>');
    setTimeout(yourFunction, 5000);
  }
  if (res == '5') {
    $('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
  }
}

function yourFunction() {
    // your code here
}

答案 1 :(得分:0)

success: function(res) {
    if (res == '1') {
        $('#show_subscriber_msg').html('<div class=gen>Thanks for your interest in Water Lily Pond. We are going to get in touch with you very soon. </div>');
        setTimeout(function() {
            $('#show_subscriber_msg').html('');
        }, 5000);
    }
    if (res == '5') {
        $('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
    }
}

答案 2 :(得分:0)

success函数中,我会添加setTimeout,如下所示:

success: function(res) {
  if (res == '1') {
    $('#show_subscriber_msg').html('<div class=gen>Thanks for your interest in Water Lily Pond. We are going to get in touch with you very soon. </div>');
    setTimeout(function() {
      // do what you want, for example:
      $('#show_subscriber_msg').html('');
    // this number is in milliseconds
    }, 500);
  }
}

数字500是在执行代码之前等待的毫秒数

答案 3 :(得分:0)

您可以使用setTimeout在几秒钟后删除$('#show_subscriber_msg')的内容

success: function(res) {
          if (res == '1') {
            $('#show_subscriber_msg').html('<div class=gen>Thanks for your interest in Water Lily Pond. We are going to get in touch with you very soon. </div>');
          }
      if (res == '5') {
            $('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
          }
setTimeout(function(){
  $('#show_subscriber_msg').empty()
},5000)
        }

empty是一个jquery方法,用于删除所有子节点

答案 4 :(得分:0)

尝试下面对我的工作:

   success: function(response) {
if (response) {
              $('#div_id').hide().html('Write Your message here').fadeIn('slow').delay(6000).hide(1);
             } 
      }

答案 5 :(得分:0)

如果你想隐藏它,你也可以使用jQuery delay或setTimout

$("#show_subscriber_msg").html('your html').delay(1000).fadeOut();

setTimeout(function() { // your code to modify or hide the message div }, 1000)