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; }
I hope this article will be helpful to everyone’s JavaScript programming design.