jQuery 应该 $(form).submit(); 不在表单标签内触发onSubmit?
我有以下情况:
<script type="text/javascript">
function CancelFormButton(button) {
$(button.form).submit();
}
</script>
<form onsubmit="alert('here');">
<input type="button" value="Cancel" onClick="CancelFormButton(this);" />
</form>
当我单击“取消”按钮时,不会触发表单标记的 onsubmit。
此行成功提交表单: $(button.form).submit();
但跳过表单标记中 onsubmit 中的 alert('here');
。
这是正确的还是我做错了什么?
顺便说一句,在这种情况下,我想要这个功能,但我只是想知道我是否会在触发 onsubmit 的浏览器中遇到问题。
I have the following:
<script type="text/javascript">
function CancelFormButton(button) {
$(button.form).submit();
}
</script>
<form onsubmit="alert('here');">
<input type="button" value="Cancel" onClick="CancelFormButton(this);" />
</form>
When I click the "Cancel" button, the onsubmit from the form tag is not triggered.
This line instead submits the form successfully: $(button.form).submit();
but skips the alert('here');
within the onsubmit in the form tag.
Is this correct or am I doing something wrong?
By the way, in this case, I want this functionality, but I'm just wondering if I'm going to run into a problem in a browser where the onsubmit is triggered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
抱歉,误解了你的问题。
根据 Javascript - 调用 form.submit() 时捕获 onsubmit:
(强调已添加)。
注意:“由用户激活”还包括点击提交按钮(可能包括回车键的默认提交行为,但我还没有尝试过)。 我相信,如果您(使用代码)单击提交按钮,它也不会被触发。
Sorry, misunderstood your question.
According to Javascript - capturing onsubmit when calling form.submit():
(emphasis added).
Note: "activated by a user" also includes hitting submit buttons (probably including default submit behaviour from the enter key but I haven't tried this). Nor, I believe, does it get triggered if you (with code) click a submit button.
此解决方法将解决@Cletus发现的问题。
将适用于所有现代浏览器。
将跨选项卡/生成的子窗口工作(是的,即使在 IE<9 中)。
并且是香草!
只需向其传递对表单元素的 DOM 引用,它就会确保所有附加的侦听器、onsubmit 以及(如果那时没有阻止)最后提交表单。
This work around will fix the issue found by @Cletus.
Will work on all modern browsers.
Will work across tabs/spawned child windows (yes, even in IE<9).
And is in vanilla!
Just pass it a DOM reference to a form element and it'll make sure all the attached listeners, the onsubmit, and (if its not prevented by then) finally, submit the form.
我认为在某些情况下希望这种行为是合理的,但我认为不触发表单提交的代码应该(始终)是默认行为。 假设您想捕获表单的提交
,然后对输入进行一些验证。 如果代码可以触发提交处理程序,则您永远不能包含
在此处理程序中,因为这会导致无限循环(将调用相同的处理程序,阻止提交、验证并重新提交)。 您需要能够在提交处理程序内手动提交表单,而不触发相同的处理程序。
我意识到这并不能直接回答问题,但是此页面上还有很多关于如何破解此功能的其他答案,我觉得需要指出这一点(并且评论太长)。
I suppose it's reasonable to want this behavior in some cases, but I would argue that code not triggering a form submission should (always) be the default behavior. Suppose you want to catch a form's submission with
and then do some validation on the input. If code could trigger submit handlers, you could never include
inside this handler because it would result in an infinite loop (the SAME handler would be called, prevent the submission, validate, and resubmit). You need to be able to manually submit a form inside a submit handler without triggering the same handler.
I realize this doesn't ANSWER the question directly, but there are lots of other answers on this page about how this functionality can be hacked, and I felt that this needed to be pointed out (and it's too long for a comment).
我的简单解决方案:
这对我来说是工作。
My simple solution:
It is work for me.
trigger('submit') 不起作用。因为 onSubmit 方法不会被触发。
以下代码适用于我,使用此代码时调用 onSubmit 方法:
$("form").find(':submit').click();
trigger('submit') does not work.beacuse onSubmit method does not get fired.
the following code works for me, call onSubmit method when using this code :
$("form").find(':submit').click();
尝试在您的函数中触发()事件:
Try to trigger() event in your function:
而不是
$("form").submit()
尝试这个
Instead of
$("form").submit()
try this
我几年前就发现了这个问题。
最近我尝试“重写”提交方法。 下面是我的代码
}
它在 IE 中执行,但在其他浏览器中失败,原因与“cletus”相同
I found this question serval years ago.
recently I tried to "rewrite" the submit method. below is my code
}
It did in IE,but failed in other browsers for the same reason as "cletus"
解决此问题的最简单解决方案是创建类型为“提交”并触发单击的“临时”输入:
The easiest solution to workaround this is to create 'temporary' input with type submit and trigger click: