javascript removeChild(this)from input [type =“submit”] onclick打破未来在firefox下使用form.submit() [英] javascript removeChild(this) from input[type="submit"] onclick breaks future use of form.submit() under firefox

查看:140
本文介绍了javascript removeChild(this)from input [type =“submit”] onclick打破未来在firefox下使用form.submit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些奇怪的行为,而且在点击事件中从中删除DOM中的输入提交元素时,我在Firefox中发生错误。

I have come across some strange behaviour, and I'm assuming a bug in firefox, when removing a input submit element from the DOM from within the click event.

以下代码重现了这个问题:

The following code reproduces the issue:

<form name="test_form">
<input type="submit" value="remove me" onclick="this.parentNode.removeChild(this);" />
<input type="submit" value="submit normally" />
<input type="button" value="submit via js" onclick="document.test_form.submit();" />
</form>

要复制:


  • 点击删除我

  • 点击通过js提交。请注意,表单没有提交,这是问题。

  • 点击正常提交。请注意,表单仍然正常提交。

看起来,在Firefox下,如果您从点击事件中删除提交按钮它将表单置于无效状态,以便将来对form.submit()的任何调用都被忽略。但是这是一个特定于JavaScript的问题,因为这种形式中的正常提交按钮仍然可以正常工作。

It appears that, under Firefox, if you remove a submit button from within the click event it puts the form in an invalid state so that any future calls to form.submit() are simply ignored. But it is a javascript-specific issue as normal submit buttons within this form still function fine.

说实话,这是一个这样一个简单的例子,期待互联网与其他人一起活跃,但到目前为止,搜索已经没有任何用处。

To be honest, this is such a simple example of this issue that I was expecting the internet to be awash with other people exeriencing it, but so far searching has yealded nothing useful.

有没有人经历过这个,如果是,你是否到达底部?

Has anyone else experienced this and if so, did you get to the bottom of it?

非常感谢

推荐答案

似乎是相关的事实上,您正在处理事件时删除节点。

这确实看起来像是Firefox的错误。

Seems to be related to the fact that you're removing the node while processing the event.
This indeed looks like a bug from Firefox.

同时,这个黑客似乎工作,但延迟删除:

In the meanwhile, this hack seems to work but delaying the removal:

<script type="text/javascript">
function delsubmit(el) {
    window.setTimeout(function() {
        el.parentNode.removeChild(el);
    }, 50);

    return false;
}
</script>

<input type="submit" value="remove me" onclick="return delsubmit(this)" />

这篇关于javascript removeChild(this)from input [type =“submit”] onclick打破未来在firefox下使用form.submit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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