I'm working on an Ajax query recently, with the following code:
1 |
< form name = "keywordForm" method = "post" action = "" > |
3 |
< label for = "profile" > keyword search: </ label > |
4 |
< input style = "WIDTH:80; Height:20 " type = " text " name = "keyword" onkeypress = /> |
5 |
< input type = "button" value = "搜索" onClick = "searchKeyword()" > |
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 |
< input id = "hiddenText" type = "text" style = "display:none" /> |
Now the code goes like this:
1 |
< form name = "keywordForm" method = "post" action = "" > |
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 |
< input type = "button" value = "搜索" onClick = "searchKeyword()" > |
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 |
< form name = "keywordForm" method = "post" action = "" onsubmit = "return false;" > |
3 |
< label for = "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 |
< input type = "button" value = "搜索" onClick = "searchKeyword()" > |
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