form.submit()方法中的setTimeout不起作用

我在表单提交中尝试setTimeout()函数。 但是,在执行该函数之前,表单将被提交,并且超时函数内的任何内容都不会被执行。 以下是代码段。

 $(window).load(function () { $("#myform").submit(function() { setTimeout(function(){ alert("me after 1000 mili seconds"); return false; }, 1000); }); }; 

请帮我解决这个问题……

 $(window).load(function () { var submit = false; $("#myform").submit(function(e) { setTimeout(function(){ alert("me after 1000 mili seconds"); submit = true; $("#myform").submit(); // if you want }, 1000); if(!submit) e.preventDefault(); }); }; 

表格即将提交。 您需要委派默认操作。 这应该适合你:

 $(window).load(function () { $("#myform").submit(function(e) { e.preventDefault(); setTimeout(function(){ alert("me after 1000 mili seconds"); }, 1000); }); }); 

请看这个jsFiddle演示

如果要禁用表单上的提交操作,可以尝试:

 $(window).load(function () { $("#myform").submit(function(e) { e.preventDefault(); setTimeout(function(){ alert("me after 1000 mili seconds"); return false; }, 1000); }); }); 

首先,你在最后一行忘了关闭括号

 $(window).load(function () { $("#myform").submit(function(e) { e.preventDefault(); setTimeout(function(){ alert("me after 1000 mili seconds"); return false; }, 1000); }); }); ^---- here you forgotten to put brace.