HTML Web page Form form has only one input text element, press ENTER default commit

Source: Internet
Author: User

In web development, if there is only one INPUT element in the form of the page, press ENTER in the input box of the INPUT element (note: There is no event processing to write the corresponding onkeydown, etc.), then the browser will submit the form by default, see the following code:

<HTML>      <Head>         <title>Default Submission Form when there is only one INPUT element on the page</title>      </Head>      <Body>           <formAction= "Http://www.baidu.com"name= "MyForm"Method= "Get">              <inputname= "Contenta"type= "text"value= "Enter Submit" /></BR>           </form>      </Body>  </HTML>  

So how to prohibit, there are two methods of prohibition:

[1] Only one occurrence, you can add a hidden INPUT element in the form, the modified code is as follows:

<HTML>      <Head>         <title>Default Submission Form when there is only one INPUT element on the page</title>      </Head>      <Body>           <formAction= "Http://www.baidu.com"name= "MyForm"Method= "Get">              <inputname= "Contenta"type= "text"value= "Enter Submit" /></BR>            <inputtype= "text"style= "Display:none">           </form>      </Body>  </HTML>  

[2] Disabling the onsubmit event for form:

<HTML>      <Head>          <title>Default Submission Form when there is only one INPUT element on the page</title>      </Head>      <Body>           <formonsubmit= "return false;"Action= "XXX"name= "MyForm"Method= "Get">              <inputname= "Contenta"type= "text"value= "Enter to Submit" /></BR>           </form>      </Body>  </HTML> <Scripttype= "Text/javascript">functionUpdate () {document.forms[0].action="${contextpath}updateaction.action"; document.forms[0].submit ();}</Script>

HTML Web page Form form has only one input text element, press ENTER default commit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.