gpt4 book ai didi

javascript - 检测文本输入字段中的 Enter 键

转载 作者:IT老高 更新时间:2023-10-28 13:12:01 30 4
gpt4 key购买 nike

如果在特定输入时按下回车,我正在尝试执行一个功能。

我做错了什么?

$(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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com