【问题标题】:IE firing submit on button even there is no <form> tag即使没有 <form> 标签,IE 也会在按钮上触发提交
【发布时间】:2013-10-04 13:51:19
【问题描述】:

我知道,如果我们有一个 &lt;form&gt; 和一个带有或不带有 submit 按钮的 &lt;input&gt;,按下 Enter 键将提交该封闭表单。但是,即使不存在&lt;form&gt; 标签,IE 也会提交(特别是单击它遇到的第一个按钮)。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <button onclick="alert('button 1 clicked')">button 1</button>
    <button onclick="alert('button 2 clicked')">button 2</button>
    <input type="text" />
</body>
</html>

Plunkr 中的标记上方。

当在 input 字段中按下“Enter”时,将看到“button 1 clicked”警报。

如果我用&lt;form&gt; 标记包围&lt;input&gt;,则表示不提交。

Plunkr

在 IE9 和 IE 10 中也观察到了这种行为(尚未测试其他版本)。这是 IE 中的错误,或者如果不是,如何阻止这种情况发生?

更新:如果我添加type="button",则不会观察到上述行为。 &lt;button&gt; 标签的默认行为是 submit 吗?而且,只有 IE 有这样的行为?

【问题讨论】:

标签: forms internet-explorer internet-explorer-9 internet-explorer-10 form-submit


【解决方案1】:

如果我添加 type="button",则不会观察到上述行为。标签的默认行为是提交吗?而且,只有 IE 有这样的行为?

This answer 帮我确定添加type="button" 不会造成这种影响

【讨论】:

    【解决方案2】:

    您可以通过在 onclick 事件中添加 'return false;' 来阻止 IE 提交表单。这是代码:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
       <button onclick="alert('button 1 clicked'); return false; ">button 1</button>
       <button onclick="alert('button 2 clicked'); return false; ">button 2</button>
       <input type="text" />
    </body>
    </html>
    

    此外,您还可以通过添加 'return false' 来阻止提交按钮提交表单。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2011-10-24
    • 2022-01-20
    相关资源
    最近更新 更多