jQuery的$(form).submit();应该吗?不在表单标签内触发onSubmit吗? [英] Should jQuery's $(form).submit(); not trigger onSubmit within the form tag?

查看:141
本文介绍了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不会被触发.

When I click the "Cancel" button, the onsubmit from the form tag is not triggered.

该行改为成功提交表单:$(button.form).submit();,但跳过了表单标签中onsubmit中的alert('here');.

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?

在这种情况下,我需要这种功能,但是我只是想知道是否会在触发onsubmit的浏览器中遇到问题.

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.

推荐答案

对不起,误解了您的问题.

Sorry, misunderstood your question.

根据 Javascript-调用form.submit()时捕获onsubmit:

According to Javascript - capturing onsubmit when calling form.submit():

最近有人问我:为什么不 我触发form.onsubmit事件 使用javascript提交我的表单吗?"

I was recently asked: "Why doesn't the form.onsubmit event get fired when I submit my form using javascript?"

答案:当前的浏览器不支持 遵守html的这一部分 规格.该事件仅触发 由用户激活时- 被激活时不会触发 代码.

The answer: Current browsers do not adhere to this part of the html specification. The event only fires when it is activated by a user - and does not fire when activated by code.

(添加了重点).

注意:由用户激活"还包括点击提交按钮(可能包括Enter键中的默认提交行为,但我没有尝试过).我相信,如果您(使用代码)单击提交按钮,也不会触发该事件.

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.

这篇关于jQuery的$(form).submit();应该吗?不在表单标签内触发onSubmit吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆