为什么将 HTML 表单提交按钮命名为“提交”? 打破东西?

发布于 2024-07-19 08:36:25 字数 923 浏览 6 评论 0原文

在 ASP.NET Webforms 和 ASP 3(经典 ASP)中,我遇到了一个问题,将表单提交按钮命名为“提交”会“破坏一切”。 下面是呈现的 HTML:

<input type="submit" name="Submit" value="Submit" id="Submit" />

我说“破坏事物”,因为我不确定到底为什么或发生了什么。 但症状通常是按提交按钮有时没有任何反应,即不起作用。 但有时它确实有效。

事实上,我只是用下面的代码构建了一个快速的一页测试,并且提交工作正常:

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtTest" runat="server" />
    <asp:Button ID="Submit" runat="server" Text="Submit" />
</div>
</form>

但是,在过去,这个问题已经出现过,并且重命名按钮总是会让症状消失。

那么,任何 HTML/HTTP/浏览器专家是否知道为什么在“提交”按钮上设置 id="submit" 会导致任何问题?

编辑

SO评论似乎表明“submit”是一个保留关键字。 但是为什么“id”或“name”属性会与此相干扰呢? 这个“保留”关键字是如何以导致冲突的方式实现的?

再次感谢

In ASP.NET webforms and ASP 3 (Classic ASP), I came across an issue whereby naming your form submit button "submit" would "break things". Below is the rendered HTML:

<input type="submit" name="Submit" value="Submit" id="Submit" />

I say "break things" because I'm not sure exactly why or what happened. But the symptoms usually were that pressing the submit button sometimes did nothing i.e. it just didn't work. But sometimes it did work.

In fact, I just built a quick one page test with the the code below, and submitting worked fine:

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtTest" runat="server" />
    <asp:Button ID="Submit" runat="server" Text="Submit" />
</div>
</form>

But, in the past, this problem has arisen, and renaming the button always made the symptom go away.

So, does any HTML/HTTP/Browser expert know of any reason why setting id="submit" on a Submit button would cause any problems?

EDIT

this SO comment seems to suggest "submit" is a reserved keyword. But why would the "id" or "name" attributes intefere with this? And how does this "reserved" keyword get implemented in such a way that would cause conflicts?

thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

俯瞰星空 2024-07-26 08:36:25

form 元素有一个名为 submit 的方法,而且还具有表单中的表单元素作为成员。

如果表单中有一个名为 submit 的按钮,则可以使用 document.form1.submit 访问它。 但是,由于它与 submit 方法同名,因此不再有任何方法可以访问该方法。 如果您使用该方法提交表单,则不再有效。

例如,如果您有一个使用 Javascript 提交表单的按钮,则该按钮不起作用:

<input type="button" name="submit" onclick="this.form.submit();" value="try" />

当该按钮尝试使用 submit 方法时,它会获取对其自身的引用(以及一个错误尝试调用它时的消息,因为该按钮不是函数)。

The form element has a method named submit, but also has the form elements in the form as members.

If you have a button in the form named submit, you could access it using document.form1.submit. However, as that is the same name as the submit method, there is no longer any way of accessing that method. If you use the method to submit the form, that will no longer work.

For example, if you have a button that submits the form using Javascript, that doesn't work:

<input type="button" name="submit" onclick="this.form.submit();" value="try" />

When the button tries to use the submit method, it will instead get a reference to itself (and an error message when trying to call it, as the button is not a function).

花开雨落又逢春i 2024-07-26 08:36:25

我强烈建议您远离 javascript 的 DOM 保留字。 在您定义的所有内容、$ 或其他显然不会与保留字冲突的内容前面使用“my”,否则您可能会意外地超载并造成严重破坏。

I would urge you to stay away from all of javascript's DOM's reserved words. use "my" in front of everything you define, or $, or something else that is clearly not going to conflict with the reserved words that you can accidentally just overload and cause havoc.

木槿暧夏七纪年 2024-07-26 08:36:25

如果表单提交出现问题,只需使用 onsubmit

<form id="form1" runat="server" onsubmit="this.submit()">
<div>
    <asp:TextBox ID="txtTest" runat="server" />
    <asp:Button ID="Submit" runat="server" Text="Submit" Type="submit"/>
</div>
</form>

Just use onsubmit if proble in form submission

<form id="form1" runat="server" onsubmit="this.submit()">
<div>
    <asp:TextBox ID="txtTest" runat="server" />
    <asp:Button ID="Submit" runat="server" Text="Submit" Type="submit"/>
</div>
</form>
满天都是小星星 2024-07-26 08:36:25

生成的代码隐藏允许您将事件绑定到对象,并根据您指定的值进行命名。

The code-behind that is generated that allows you to bind events to the object are named based on the values you specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文