如果在特定输入时按下回车,我正在尝试执行一个功能。
我做错了什么?
$(document).keyup(function (e) {
if ($(".input1").is(":focus") && (e.keyCode == 13)) {
// Do something
}
});
有没有更好的方法可以做到这一点,如果在 .input1
上按下输入,则执行功能?
最佳答案
$(".input1").on('keyup', function (e) {
if (e.key === 'Enter' || e.keyCode === 13) {
// Do something
}
});
// e.key is the modern way of detecting keys
// e.keyCode is deprecated (left here for for legacy browsers support)
// keyup is not compatible with Jquery select(), Keydown is.
关于javascript - 检测文本输入字段中的 Enter 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060750/