如果 e.keyCode 是 "enter" 按钮并且元素包含其他文本,则条件已验证
If e.keyCode is the "enter" button and the element contains other text the condition is verified
我在 input
元素上使用 keyup()
方法:
我想知道是否可以做到按下的按钮是 enter
但是 输入字段不为空然后提醒一个变量。我试过了:
$('#search').keyup(function(e) { //#search is id input
if (($('#search').val() != "") && (e.keyCode == 13)) {
alert($('#search').val()); //alert
$(this).closest('form').submit();
}
});
不起作用...因为如果输入元素为空,条件也为真:/
如果你想帮助我,这是 jsfiddle:
https://jsfiddle.net/0br6q72r/
对不起我的英语,非常感谢! :)
您必须改用 keydown
事件,如果值包含空格,则必须 .trim()
ed 值:
$('#search').keydown(function(e) {
if (this.value.trim().length == 0 && e.keyCode == 13) { // <--ensures if no values entered and hit enter key
e.preventDefault(); // then stop the process
} else if (e.keyCode == 13) { // check if key is enter, it is required
// as it enable user to put search inputs.
alert($('#search').val()); // then alert the value
$(this).closest('form').submit(); // and submit the form
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='http://www.google.com' accept-charset="UTF-8" method="get" target='_blank' id='cse-search-box'>
<input type='text' id="search" name='q' size='50' />
</form>
Fiddle.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#search').keyup(function (e) { //#search is id input
if (($('#search').val().trim() != "") && (e.keyCode == 13)) {
alert($('#search').val()); //alert
// $(this).closest('form').submit();
}
});
});
</script>
</head>
<body>
<div>
<input type="text" id="search" />
</div>
</body>
</html>
我在 input
元素上使用 keyup()
方法:
我想知道是否可以做到按下的按钮是 enter
但是 输入字段不为空然后提醒一个变量。我试过了:
$('#search').keyup(function(e) { //#search is id input
if (($('#search').val() != "") && (e.keyCode == 13)) {
alert($('#search').val()); //alert
$(this).closest('form').submit();
}
});
不起作用...因为如果输入元素为空,条件也为真:/
如果你想帮助我,这是 jsfiddle: https://jsfiddle.net/0br6q72r/
对不起我的英语,非常感谢! :)
您必须改用 keydown
事件,如果值包含空格,则必须 .trim()
ed 值:
$('#search').keydown(function(e) {
if (this.value.trim().length == 0 && e.keyCode == 13) { // <--ensures if no values entered and hit enter key
e.preventDefault(); // then stop the process
} else if (e.keyCode == 13) { // check if key is enter, it is required
// as it enable user to put search inputs.
alert($('#search').val()); // then alert the value
$(this).closest('form').submit(); // and submit the form
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='http://www.google.com' accept-charset="UTF-8" method="get" target='_blank' id='cse-search-box'>
<input type='text' id="search" name='q' size='50' />
</form>
Fiddle.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#search').keyup(function (e) { //#search is id input
if (($('#search').val().trim() != "") && (e.keyCode == 13)) {
alert($('#search').val()); //alert
// $(this).closest('form').submit();
}
});
});
</script>
</head>
<body>
<div>
<input type="text" id="search" />
</div>
</body>
</html>