Why does naming your HTML form submit button “submit” break things?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 11:36:28

问题


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


回答1:


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




回答2:


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.




回答3:


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




回答4:


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>


来源:https://stackoverflow.com/questions/876243/why-does-naming-your-html-form-submit-button-submit-break-things

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!