When there is only one input text in the FORM in JSP, press enter to automatically submit the FORM, forminput

Source: Internet
Author: User

When there is only one input text in the FORM in JSP, press enter to automatically submit the FORM, forminput

A list interface has only one query condition for the input box. When you enter Chinese characters in the input box for the first time, press the Enter key to find that Chinese characters in the input box are garbled! I thought it was a very simple problem, but it took a long time to find the cause (it is said that it was a browser problem). I pressed enter and executed two queries.

1. Code before modification:

<form id="ff" name="ff" method="post">   <input type="text" id="userName" name="userName" size="12" maxlength='30' value="${(data.userName)!''}" onKeypress= "javascript:if(event.keyCode==13) do_postAuditList_search();"/> </form> 

2. modified code:

<Form id = "ff" name = "ff" method = "post"> <input type = "text" id = "userName" name = "userName" size = "12" maxlength = '30' value = "$ {(data. userName )! ''}" OnKeypress = "javascript: if (event. keyCode = 13) do_postAuditList_search ();"/> <! -- Solve the problem that the form will be automatically submitted when you press enter in only one input box --> <input style = 'display: none'/> </form>

That is, add<input type='text' style='display:none'/>The input box is not displayed, and will not be submitted after you press Enter.

Supplement:

Automatic submission may occur in two ways:

First, javascript code is compiled. When you press Enter, the js event listening mechanism is used to trigger form submission.

Second, the browser's default behavior is used (at least ie is found to be like this ). When a browser parses a webpage, there are many default actions. For example, if a page contains a form and a submit button, the focus will automatically fall on the submit button when the page is opened. Similarly, if a form contains only one text field (text), the browser automatically submits the form when the Enter key is pressed in the input field.

We generally know the first case and it is easy to understand, but for the default behavior of the second browser, there may be fewer people who know this, next I will take a look at the default behavior of the browser (at least ie) in Form submission.
If a form contains a single line of text input fields, no matter how many other types of form components are contained, the form is automatically submitted when you click Enter in the input field. For example, the following code:

<form action="" method="post"><input type="text" name="sdfsdf"/><textarea></textarea><input type="checkbox">sdfsdf<input type="hidden" name="aa"/></form>

If a form contains two or more single-line text input fields, no matter whether it contains other types of form components, it is not automatically submitted when you press Enter. For example:

<form action="" method="post"><input type="text" name="sdfsdf"/><input type="text" name="sddf"/></form>

The solution is simple. We have already used the above example. You only need to add another text input box. You may say that you need to add a useless input box in order not to submit the box automatically, and it contains two input boxes. Will the end user accept them? You can hide the newly added input box by using style. For example:

<form action="" method="post"><input type="text" name="notautosubmit" style="display:none"/><input type="text" name="username"/></form>

You can also bind the button enter to trigger the event:

document.onkeypress = function(){if(event.keyCode == 13) {search();return false;}}

Here, the search method is an onclick event:<form name="searchfrom">

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.