Home  >  Article  >  Web Front-end  >  js光标定位文本框回车表单提交问题的解决方法_javascript技巧

js光标定位文本框回车表单提交问题的解决方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 15:59:521193browse

本文实例讲述了js光标定位文本框回车表单提交问题的解决方法。分享给大家供大家参考。具体分析如下:

当光标定位在辅助查找的文本框后回车,页面会出现方法的返回的json串。

原因: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.

翻译一下:当form中只有一个input type="text"时,当用户按回车键会提交这个form。

解决方案:对input text的onkeydown事件做处理,禁止回车操作。

具体代码:

<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;
}

希望本文所述对大家的javascript程序设计有所帮助。

Statement:
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