递归 jQuery 提交函数不提交表单
我需要一些代码在单击提交按钮之后和实际提交表单之前运行。 我已经删除了代码,只留下了向您展示问题所需的部分。 我需要代码做的是进入提交函数,但不是提交表单,而是再次调用该函数。 该表单应仅在第二次迭代中提交。 但事实并非如此。代码已按预期完全执行,但表单最终未提交。
如果有人能解释原因,我将不胜感激。
这是代码(simplr-reg 是表单的名称):
<script>
var reallySubmit = false;
$(‘#simplr-reg’).submit(function(event)
{
if (false == reallySubmit)
{
reallySubmit = true;
$(‘#simplr-reg’).submit();
event.preventDefault();
return false;
}
else
{
return true;
}
});
</script>
I need some code to run right after the submit button has been clicked and before the form has actually been submitted.
I've stripped the code and left only the parts needed to show you the problem.
What I need the code to do is go inside the submit function, but instead of submitting the form call the function again.
The form should submit only in that second iteration.
Except it doesn't. The code is fully executed as it should but the form doesn't submit in the end.
I would appreciate it if someone can explain why.
Here's the code (simplr-reg's the form's name):
<script>
var reallySubmit = false;
$(‘#simplr-reg’).submit(function(event)
{
if (false == reallySubmit)
{
reallySubmit = true;
$(‘#simplr-reg’).submit();
event.preventDefault();
return false;
}
else
{
return true;
}
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这表示“在 jQuery 选择上调用
submit
方法”。您有一个绑定到所选内容的 jQuerysubmit
处理程序。由于您已经触发了 jQuery 选择的事件,因此 jQuery 处理程序也会被触发。如果您在本机 DOM 表单对象(提交处理程序中的
this
)上触发事件,则不会触发 jQuery 处理程序,因此您不会原地踏步。调用form.submit
方法:That says "call the
submit
method on the jQuery selection". You have a jQuerysubmit
handler bound to the selection. Since you have triggered the event on the jQuery selection, the jQuery handler is also triggered.If you trigger the event on the native DOM form object (
this
within the submit handler), the jQuery handler will not be triggered, so you won't go round in circles. Call theform.submit
method:将其从“提交”事件更改为“单击”事件并将其附加到按钮本身而不是表单?
预计到达时间:请务必在未提交的情况下“返回 false”。
Change it from a 'submit' event to a 'click' event and attach it to the button itself rather than the form?
ETA: be sure to 'return false' on your non-submit case.