Home > Web Front-end > JS Tutorial > body text

Solution to the problem of js cursor positioning text box and carriage return form submission_javascript skills

WBOY
Release: 2016-05-16 15:59:52
Original
1251 people have browsed it

The example in this article describes the solution to the problem of js cursor positioning text box and carriage return form submission. Share it with everyone for your reference. The specific analysis is as follows:

When the cursor is positioned in the text box of the auxiliary search and press Enter, the json string returned by the method will appear on the page.

Reason: When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.

Translation: When there is only one input type="text" in the form, the form will be submitted when the user presses the Enter key.

Solution: Process the onkeydown event of input text and disable the carriage return operation.

Specific code:

<p>
<input class="text text-1" type="text" name="name" 
id="notAssociateName" value="" onkeydown="enter_down(event);"/>
</p>
function enter_down(event){
 if(event.keyCode==13){
  stopDefault(event);
 }
}
function stopDefault(e) {
 //如果提供了事件对象,则这是一个非IE浏览器
 if(e && e.preventDefault) {
   //阻止默认浏览器动作
   e.preventDefault();
 } else {
   //IE中阻止函数器默认动作的方式
   window.event.returnValue = false;
 }
 return false;
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template