Keyboard return event causes page refresh issues

Source: Internet
Author: User

I'm working on an Ajax query recently, with the following code:

1 <formname="keywordForm" method="post"action=""
2 <pid="profile_nav"
3 < label   for = "profile" > keyword search: </ label
4 < input   style = "WIDTH:80; Height:20 "   type = " text "   name = "keyword" onkeypress =  /> 
5 <inputtype="button" value="搜索"onClick="searchKeyword()"
6 </p>
7 </form>

Enter the keyword in the text box press ENTER, the page automatically refreshed, the results must not be implemented without a refresh search. Think, may be to press ENTER after the default submission of the form, so the form is removed, sure enough not to brush. But there are still a lot of places that need to use form.

Under a form, if there is only one text box, pressing ENTER will touch the submit event of the publication order.

Since there is only one text box that will cause problems, you can add a hidden text box as follows:

1 <inputid="hiddenText" type="text" style="display:none"/>

Now the code goes like this:

1 <formname="keywordForm" method="post"action=""
2 <pid="profile_nav"
3 < label   for = "profile" > keyword search: </ label
4 < input   style = "WIDTH:80; Height:20 "   type = " text "   name = "keyword" onkeypress =  /> 
5 < input   id = "Hiddentext"   type = Code class= "string" > "text"   style = "Display:none" onkeypress =  />
6 <inputtype="button" value="搜索"onClick="searchKeyword()"
7 </p>
8 </form>

The conclusion is that two methods can be adopted to solve the problem: 1. Remove the form; 2. If you have to use a form, just leave it alone and only one text box is OK.

If the above method is not enough for you to solve the problem, then you can use the following methods to prevent the automatic submission of forms caused by carriage return:

1 <formname="keywordForm" method="post" action=""onsubmit="return false;"
2 <pid="profile_nav"
3 <labelfor="profile"> 关键字搜索: </label
4 < input   style = "WIDTH:80; Height:20 "   type = " text "   name = "keyword" onkeypress =  /> 
5 < input   id = "Hiddentext"   type = Code class= "string" > "text"   style = "Display:none" onkeypress =  />
6 <inputtype="button" value="搜索"onClick="searchKeyword()"
7 </p>
8 </form>

is to add a onsubmit event to the form form and return false to prevent the form from committing.

Reprinted from: http://www.nowamagic.net/html/html_EnterCouseReflesh.php

Keyboard return event causes page refresh issues

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.