为什么命名HTML表单提交按钮“提交”打破东西? [英] Why does naming your HTML form submit button "submit" break things?

查看:271
本文介绍了为什么命名HTML表单提交按钮“提交”打破东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET webforms和ASP 3(经典ASP)中,我遇到了一个问题,即命名表单提交按钮submit会破坏事物。以下是呈现的HTML:

 < input type =submitname =Submitvalue =Submitid = 提交/> 

我说破坏事物,因为我不确定发生了什么或发生了什么。但是症状通常是按下提交按钮有时什么都不做,即它不起作用。但有时它确实有用。

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

 < form id =form1runat =server> 
< div>
< asp:TextBox ID =txtTestrunat =server/>
< asp:Button ID =Submitrunat =serverText =Submit/>
< / div>
< / form>

,过去这个问题已经出现,并且重命名因此,任何HTML / HTTP /浏览器专家都知道为什么在提交时设置id =submit的任何原因按钮会导致任何问题?
$ b 编辑

一个href =https://stackoverflow.com/questions/729330/jquery-triggersubmit-breaking/749706#749706>所以评论似乎建议提交是一个保留关键字。但为什么id或name属性会干扰这个?那么这个保留关键字是如何以这样的方式实现的?会导致冲突吗?



再次感谢

表单元素有一个名为 submit 的方法,但也有如果你在名为 submit 的表单中有一个按钮,你可以访问这个表单中的元素。 它使用 document.form1.submit 。但是,因为它与 submit 方法的名称相同,所以不再有任何方式访问该方法。如果您使用该方法提交表单,这将不再有效。



例如,如果您有一个使用Javascript提交表单的按钮, work:

 < input type =buttonname =submitonclick =this.form.submit();  value =try/> 

当按钮尝试使用提交方法,它会取而代之的是它自己的引用(当试图调用它时会产生一个错误信息,因为这个按钮不是函数)。


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

解决方案

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

这篇关于为什么命名HTML表单提交按钮“提交”打破东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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