gpt4 book ai didi

jQuery - keydown/keypress/keyup ENTERKEY 检测?

转载 作者:行者123 更新时间:2023-12-03 21:27:45 24 4
gpt4 key购买 nike

尝试让 jQuery 检测 Enter 输入,但检测到空格和其他键,未检测到 Enter。下面有什么问题:

$("#entersomething").keyup(function(e) {
alert("up");
var code = (e.keyCode ? e.keyCode : e.which);
if (code==13) {
e.preventDefault();
}

if (code == 32 || code == 13 || code == 188 || code == 186) {
$("#displaysomething").html($(this).val());
});

<input id="entersomething" />
<div id="displaysomething"&gt;&lt;/div&gt;

http://jsfiddle.net/zeRrv/

最佳答案

JavaScript/jQuery

$("#entersomething").keyup(function(e){ 
var code = e.key; // recommended to use e.key, it's normalized across devices and languages
if(code==="Enter") e.preventDefault();
if(code===" " || code==="Enter" || code===","|| code===";"){
$("#displaysomething").html($(this).val());
} // missing closing if brace
});

HTML

<input id="entersomething" type="text" /> <!-- put a type attribute in -->
<div id="displaysomething"></div>

关于jQuery - keydown/keypress/keyup ENTERKEY 检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462995/

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