为什么不应将表单元素命名为submit? [英] Why Form Elements should not be named submit?

查看:69
本文介绍了为什么不应将表单元素命名为submit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调试发现我不应该将任何表单元素命名为 name =submit,但即使在搜索后我也找不到任何好的解释原因?

I figured out through debugging that I should not name any Form Elements name="submit", but even after searching I didn't find any good explanation of why?

请参阅下面的简单代码示例:

See simple code example below:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form action="javascript:alert('submitted');" method="post" id="test-form">
            <label>Name</label>
            <input type="text" name="name-field" value="" />
            <input type="submit" name="submit" value="Submit Button" /> <!-- name should not be "submit" -->
            <p><a href="javascript: document.getElementById('test-form').submit();">Submit Link</a></p>
        </form>
    </body>
</html>




  • 如果您按Enter键表单元素或单击
    提交按钮,它将起作用。

  • 如果您单击提交链接 ,它将出错

    • If you Press Enter while on any Form Element or Click the Submit Button, it will work.
    • If you Click on the Submit Link, it will have error

      未处理的错误:'document.getElementById('test-form')。提交'不是功能

      Unhandled Error: 'document.getElementById('test-form').submit' is not a function

      如果你只是重命名你的提交按钮除了 name =submit之外的其他任何内容(即使您只是将其部分资本化)或只是删除 name =submit属性,然后提交按钮提交链接都可以使用。

      If you simply rename your Submit Button to anything other than name="submit" (even if you just capitalize some part of it) or just remove the name="submit" attribute, then both the Submit Button and Submit Link will work.

      我在最新版本的Internet Explorer,Firefox,Opera,Chrome和Safari中试过这个。所有这些都有与此相关的一致行为。

      I tried this in the latest version of Internet Explorer, Firefox, Opera, Chrome, and Safari. All of them have consistent behavior with regards to this.

      正如您在我的代码示例中所看到的,没有jQuery或任何其他JavaScript库的参与。

      As you can see in my code example, there is no involvement of jQuery or any other JavaScript library.

      我希望得到解释。谢谢

      推荐答案

      如果您在此处查看Mozilla文档: https://developer.mozilla.org/en-US/docs/DOM/HTMLFormElement

      If you check the Mozilla docs here : https://developer.mozilla.org/en-US/docs/DOM/HTMLFormElement

      你会看到表格有 .submit()方法。

      此外,表格也是填充表单中的字段。

      In addition, forms are also populated with the fields within the form.

      (以下是一个例子:
      http://www.devbay.com/articles/javascript/accessing-form-elements-array-with-javascript/
      我可以'找到它应该发生的任何引用,只发现它。)

      (Here is one example: http://www.devbay.com/articles/javascript/accessing-form-elements-array-with-javascript/ I can't find any reference that it should happen, only that it does.)

      所以,当你创建一个名为提交它覆盖了内置的表单 submit()方法 - 因为元素是,不是一个函数,你得到那个错误信息。

      So, when you make an element called submit it over-rides the forms built-in submit() method -- and since the element is, well, not a function, you get that error message.

      这篇关于为什么不应将表单元素命名为submit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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