简体   繁体   English

文本框是否考虑按Enter键作为提交?

[英]Do textboxes consider pressing the enter key as submit?

Do all textboxes ( <input type="text" ) consider "PRESSING THE ENTER KEY" as submit action? 是否所有文本框( <input type="text" )都将“按ENTER键”视为提交操作?

IF yes, do all browser supports this? 如果是,所有浏览器都支持吗?

If no, the what's wrong with my code detecting the enter key on press? 如果不是,我的代码在按下时检测Enter键有什么问题?

$('#quick-search-input').bind('keyup', function(e) {
  if ( e.keyCode === 13 || $('#the-button').val() ) {
     alert("pressed");
  }
});

This code alerts on all key presses, not only on enter. 该代码不仅在回车时还在所有按键上发出警报。 Something must be wrong. 一定有问题。 I'm using jquery 1.4 BTW and don't ask why. 我正在使用jQuery 1.4 BTW,不要问为什么。 Can't upgrade for now. 目前无法升级。

Problem 问题

you are checking if pressed key is eneter or textbox has value if any of condition is true than if condition will work 您正在检查按下的键是否为eneter或文本框是否具有条件是否为真值,或者条件是否可以工作

if ( e.keyCode === 13 || $('#the-button').val() ) {


Change it to 更改为

 if ( e.keyCode === 13) { alert("Enter Pressed"); } 

If you want value should not be empty than 如果要值不应该为空

 if ( e.keyCode === 13 && $.trim($('#the-button').val()) !== '') { alert("Enter Pressed"); } 

Minor changes in your code logic, 您的代码逻辑有微小变化,

$('#quick-search-input').bind('keyup', function(e) {
  if ( e.keyCode === 13 && $('#the-button').val()!="" ) {
     alert("pressed");
  }
});

最好使用e.which代替jQuery文档中的e.keyCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM