Do all textboxes ( <input type="text"
) consider "PRESSING THE ENTER KEY" as submit action?
IF yes, do all browser supports this?
If no, the what's wrong with my code detecting the enter key on press?
$('#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. 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
if ( e.keyCode === 13 || $('#the-button').val() ) {
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.