Press enter to submit the form in the text box. The text box form

Source: Internet
Author: User

Press enter to submit the form in the text box. The text box form

Sometimes we want to press enter in the input element to submit a form, but sometimes we do not want. For example, you want to directly press the Enter key to submit the form immediately after entering the keywords. In some complex forms, you may need to avoid incorrect input keys that trigger the form submission when the form is not completed.

To control these behaviors, you do not need to use JS. the browser has already done these operations for us. Here we will summarize several rules:

If there is a type = "submit" button in the form, the Enter key takes effect.

If there is only one input of type = "text" in the form, no matter what type the button is, the Enter key takes effect.

If the button does not use input, but a button, and no type is added, the default value is type = button in IE, and the default value is type = submit in FX.

Other form elements such as textarea and select do not affect the trigger rules. radio checkbox does not affect the trigger rules. However, in FX, the Return key is returned and the response is not returned in IE.

The input of type = "image" has the same effect as type = "submit". I don't know why such a type is designed. It is not recommended. It should be appropriate to add a background image with CSS.

In actual application, it is easy to make the form respond to the carriage return key, and ensure that there is a type = "submit" button in the form. What if there is only one text box that does not want to respond to the Enter key? My method is a bit awkward, that is, to write another meaningless text box and hide it. According to the 3rd rules, we try to explicitly declare the type when using the button to make the browser consistent.

A demo is provided to list some examples.

Run code box
<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
<Title> submit </title>
</Head>
<Body>
<H1> This demo shows whether to trigger the submit form by pressing enter in the text box <H2> by default, a text box is submitted, whether the button type is submit or button <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "button" value = "Submit">
</Form>
<H2> how can I not submit a text box by adding a hidden text box? <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "text" style = "display: none">
<Input type = "button" value = "Submit">
</Form>
<H2> as long as a button with the type of submit exists, one text box or multiple text boxes are submitted <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "submit" value = "submit">
</Form>
<H2> as long as a button with the type of submit exists, one text box or multiple text boxes are submitted <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "text">
<Input type = "submit" value = "submit">
</Form>
<H2> if you do not submit multiple text boxes, use the button with the type of button. <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "text">
<Input type = "button" value = "Submit">
</Form>
<H2> when the button element is used, FX and IE have different performances <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "text">
<Button> submit </button>
</Form>
<H2> radio and checkbox will also trigger the submission form in FX, but not in IE <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "radio" name = "a">
<Input type = "checkbox" name = "B">
<Input type = "checkbox" name = "c">
<Input type = "button" value = "Submit">
</Form>
<H2> A button whose type is image is equivalent to a button whose type is submit <Form action = "http://www.koubei.com">
<Input type = "text">
<Input type = "text">
<Input type = "image" src = "http://k.kbcdn.com/images/common/logo_yahookoubei_s.gif">
</Form>
</Body>
</Html>

Related Article

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.