Today, I encountered a very abnormal problem. Because a input box uses suggest, the carriage return event will be used.
However, in a form, when I press enter, my form is automatically submitted. I rely on it, but it cannot be changed.
You cannot change the submit parameter to a button. It looks like form. submit () is automatically executed ()
As a result, I got a man's article from the Internet. The original article is as follows:
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.
This senior person remainsDemo(Click to view) to list some examples.
Reprinted: http://shake863.javaeye.com/blog/363809