jQuery 应该 $(form).submit(); 不在表单标签内触发onSubmit?

发布于 2024-07-15 05:55:26 字数 534 浏览 13 评论 0原文

我有以下情况:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

心清如水 2024-07-22 05:55:27

抱歉,误解了你的问题。

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

最近有人问我:“为什么不
当我时 form.onsubmit 事件被触发
使用 JavaScript 提交我的表单?”

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

(强调已添加)。

注意:“由用户激活”还包括点击提交按钮(可能包括回车键的默认提交行为,但我还没有尝试过)。 我相信,如果您(使用代码)单击提交按钮,它也不会被触发。

Sorry, misunderstood your question.

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

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

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.

(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.

梦里梦着梦中梦 2024-07-22 05:55:27

解决方法将解决@Cletus发现的问题。

function submitForm(form) {
    //get the form element's document to create the input control with
    //(this way will work across windows in IE8)
    var button = form.ownerDocument.createElement('input');
    //make sure it can't be seen/disrupts layout (even momentarily)
    button.style.display = 'none';
    //make it such that it will invoke submit if clicked
    button.type = 'submit';
    //append it and click it
    form.appendChild(button).click();
    //if it was prevented, make sure we don't get a build up of buttons
    form.removeChild(button);
}

将适用于所有现代浏览器。
将跨选项卡/生成的子窗口工作(是的,即使在 IE<9 中)。
并且是香草!

只需向其传递对表单元素的 DOM 引用,它就会确保所有附加的侦听器、onsubmit 以及(如果那时没有阻止)最后提交表单。

This work around will fix the issue found by @Cletus.

function submitForm(form) {
    //get the form element's document to create the input control with
    //(this way will work across windows in IE8)
    var button = form.ownerDocument.createElement('input');
    //make sure it can't be seen/disrupts layout (even momentarily)
    button.style.display = 'none';
    //make it such that it will invoke submit if clicked
    button.type = 'submit';
    //append it and click it
    form.appendChild(button).click();
    //if it was prevented, make sure we don't get a build up of buttons
    form.removeChild(button);
}

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.

寂寞花火° 2024-07-22 05:55:27

我认为在某些情况下希望这种行为是合理的,但我认为不触发表单提交的代码应该(始终)是默认行为。 假设您想捕获表单的提交

$("form").submit(function(e){
    e.preventDefault();
});

,然后对输入进行一些验证。 如果代码可以触发提交处理程序,则您永远不能包含

$("form").submit()

在此处理程序中,因为这会导致无限循环(将调用相同的处理程序,阻止提交、验证并重新提交)。 您需要能够在提交处理程序内手动提交表单,而不触发相同的处理程序。

我意识到这并不能直接回答问题,但是此页面上还有很多关于如何破解此功能的其他答案,我觉得需要指出这一点(并且评论太长)。

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

$("form").submit(function(e){
    e.preventDefault();
});

and then do some validation on the input. If code could trigger submit handlers, you could never include

$("form").submit()

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).

暗恋未遂 2024-07-22 05:55:27

我的简单解决方案:

$("form").children('input[type="submit"]').click();

这对我来说是工作。

My simple solution:

$("form").children('input[type="submit"]').click();

It is work for me.

GRAY°灰色天空 2024-07-22 05:55:27

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();

独夜无伴 2024-07-22 05:55:27

尝试在您的函数中触发()事件:

$("form").trigger('submit'); // and then... do submit()

Try to trigger() event in your function:

$("form").trigger('submit'); // and then... do submit()
暗喜 2024-07-22 05:55:27

而不是

$("form").submit()

尝试这个

 $("<input type='submit' id='btn_tmpSubmit'/>").css('display','none').appendTo('form');
 $("#btn_tmpSubmit").click();

Instead of

$("form").submit()

try this

 $("<input type='submit' id='btn_tmpSubmit'/>").css('display','none').appendTo('form');
 $("#btn_tmpSubmit").click();
心的位置 2024-07-22 05:55:27

我几年前就发现了这个问题。

最近我尝试“重写”提交方法。 下面是我的代码

window.onload= function (){
for(var i= 0;i<document.forms.length;i++){
    (function (p){
        var form= document.forms[i];
        var originFn= form.submit;
        form.submit=function (){
            //do something you like
            alert("submitting "+form.id+" using submit method !");
            originFn();
        }
        form.onsubmit= function (){
            alert("submitting "+form.id+" with onsubmit event !");
        }
    })(i);


}

}

<form method="get" action="" id="form1">
<input type="submit" value="提交form1" />
<input type="button" name="" id="" value="button模拟提交1" onclick="document.forms[0].submit();" /></form>

它在 IE 中执行,但在其他浏览器中失败,原因与“cletus”相同

I found this question serval years ago.

recently I tried to "rewrite" the submit method. below is my code

window.onload= function (){
for(var i= 0;i<document.forms.length;i++){
    (function (p){
        var form= document.forms[i];
        var originFn= form.submit;
        form.submit=function (){
            //do something you like
            alert("submitting "+form.id+" using submit method !");
            originFn();
        }
        form.onsubmit= function (){
            alert("submitting "+form.id+" with onsubmit event !");
        }
    })(i);


}

}

<form method="get" action="" id="form1">
<input type="submit" value="提交form1" />
<input type="button" name="" id="" value="button模拟提交1" onclick="document.forms[0].submit();" /></form>

It did in IE,but failed in other browsers for the same reason as "cletus"

回梦 2024-07-22 05:55:27

解决此问题的最简单解决方案是创建类型为“提交”并触发单击的“临时”输入:

var submitInput = $("<input type='submit' />");
$("#aspnetForm").append(submitInput);
submitInput.trigger("click");

The easiest solution to workaround this is to create 'temporary' input with type submit and trigger click:

var submitInput = $("<input type='submit' />");
$("#aspnetForm").append(submitInput);
submitInput.trigger("click");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文