如何定位回车键事件?
How do I target the enter key event?
我有以下代码适用于除 enter、shift 等之外的所有键
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keypress(function(evt){
alert("hello");
});
});
为什么它在输入时不起作用?这就是我想要它做的。
我试过 evt.char、evt.keyCode evt.which 但没有任何效果。
您需要使用 KeyDown()
事件,它会触发所有特殊键,包括 enter、alt、shift 等...
检查此代码:
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keydown(function(evt){
console.log('Key pressed : ' + evt.keyCode)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="input" name="tmp_post_tag" placeholder="press key here" />
试试这个代码。 13代表回车键。
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keypress(function(evt){
if(evt.which == 13) {
alert("hello");
}
});
});
我有以下代码适用于除 enter、shift 等之外的所有键
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keypress(function(evt){
alert("hello");
});
});
为什么它在输入时不起作用?这就是我想要它做的。 我试过 evt.char、evt.keyCode evt.which 但没有任何效果。
您需要使用 KeyDown()
事件,它会触发所有特殊键,包括 enter、alt、shift 等...
检查此代码:
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keydown(function(evt){
console.log('Key pressed : ' + evt.keyCode)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="input" name="tmp_post_tag" placeholder="press key here" />
试试这个代码。 13代表回车键。
$(document).ready(function(){
$('input[name="tmp_post_tag"]').keypress(function(evt){
if(evt.which == 13) {
alert("hello");
}
});
});