简体   繁体   English

使用JQuery延迟提交表单

[英]Delayed form submission using JQuery

I am trying to delay my form submission so that when valid information is entered an alert pops up to tell the user that their information has been recorded. 我试图延迟我的表单提交,以便在输入有效信息时弹出一个警报,通知用户其信息已被记录。 The page should then wait for X seconds before submitting the form. 然后页面应等待X秒钟,然后再提交表单。

I have managed to get the alert pop-up working with one of my other forms but for some reason it is not working with this one. 我设法使警报弹出窗口与其他表单之一一起工作,但由于某种原因,它不适用于该表单。

I'll include my JQuery code below, any ideas? 我将在下面包含我的JQuery代码,有什么想法吗?

Thanks 谢谢

THIS IS THE CODE FOR THE FORM THAT WORKS: 这是工作形式的代码:

 $('#addprebooked_form').submit(function(e) { if ($('#addprebooked_email').val() != $('#addprebooked_confirmemail').val()) { $("<div class='alert alert-warning' style='position: fixed; bottom: 0;'>The emails you have enter do not appear to match! Please check you have entered the visitors details correctly and try again.</div>").appendTo('#prebook').fadeOut(3000); e.preventDefault(); return false; } else { $("<div class='alert alert-success' style='position: fixed; bottom: 0;'>Success! This visitor has been booked in for the time and date specified.</div>").appendTo('#prebook').fadeOut(3000); var delay = 3000; setTimeout(function () { $(this).submit(); }, delay); } }); 

THIS IS THE CODE I AM HAVING PROBLEMS WITH: 这是我遇到的代码:

 $('#addevent_form').submit(function(e) { if ($('#addevent_form').validate().checkForm() === false) { e.preventDefault(); return false; } else { $("<div class='alert alert-success' style='position: fixed; bottom: 0;'><p>Success! This event has been scheduled for the time and date specified.</p></div>").appendTo('#createevent').fadeOut(3000); var delay = 3000; setTimeout(function () { $(this).submit(); }, delay); } }); 

I don't think that will work, because this inside the setTimeout() handler will not refer to the form element. 我认为这行不通,因为setTimeout()处理程序中的this函数不会引用form元素。

Also calling the jQuery's submit() method will enter a recursive loop because that will again call the submit event handler. 同时调用jQuery的submit()方法将进入一个递归循环,因为它将再次调用Submit事件处理程序。

In your case, you need to prevent the form submit in the handler either way, then in the fadeOut() complete handler, you need to call the form element's submit() method. 对于您的情况,您需要以任何一种方式防止在处理程序中提交表单,然后在fadeOut()完整处理程序中,需要调用表单元素的commit()方法。

$('#addevent_form').submit(function(e) {
  e.preventDefault();
  if ($('#addevent_form').validate().checkForm()) {
    var form = this;
    $("<div class='alert alert-success' style='position: fixed; bottom: 0;'><p>Success! This event has been scheduled for the time and date specified.</p></div>").appendTo('#createevent').fadeOut(3000, function() {
      form.submit();
    });
  }
});

Demo: Fiddle 演示: 小提琴

Note: I'm not sure how the first example is working 注意:我不确定第一个示例的工作方式

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

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