在JavaScript中使用JQuery检测关键事件(输入)(在Linux平台上) [英] Detect key event (Enter) with JQuery in Javascript (on Linux platform)
问题描述
我最终发现在Linux平台上keypress比keydown或keyup更好的兼容性。我只是将keyup/keydown改为keypress,所以一切顺利。
我不知道原因是什么,但它对我来说是解决方案。感谢所有回复我问题的人。
-
我有一些代码需要检测按键事件(我必须知道何时用户按下Enter键)和JQuery,以下是Javascript中的代码:
j.input。 bind(keyup,function(l){
if(document.selection){
g._ieCacheSelection = document.selection.createRange()
}
})。bind (keydown,function(l){
//console.log(l.keyCode);
if(l.keyCode == 13){
if(l.ctrlKey){
g.insertCursorPos(\ n);
return true;
} else {
var k = d(this),
n = k.val( );
if(k.attr('intervalTime')){
// alert('can not send');
k.css('color','red')。 val('不发送太多消息')。attr('disabled','disabled')。css('color','red');
setTimeout (函数(){k.css(颜色,)VAL(n)的.attr(禁用,)聚焦()},1000);
return
if(g_debug_num [parseInt(h.buddyInfo.id)] == undefined){
g_debug_num [parseInt(h.buddyInfo.id)] = 1;
}
if(d.trim(n)){
var m = {
to:h.buddyInfo.id,
from:h.myInfo.id ,
// stype:msg,
body:(g_debug_num [parseInt(h.buddyInfo.id)] ++)+:+ n,
timestamp :( new Date ())。getTime()
};
//g.addHistory(m);
k.val();
g.trigger(sendMessage,m);
l.preventDefault();
g.sendStatuses();
k.attr('intervalTime',100);
setTimeout(function(){k.removeAttr('intervalTime')},1000);
return
}
return
}
}
它在Windows上工作正常,但在Linux上,它有时无法捕获Enter事件。有人可以帮忙吗?
已更新:
英语说话。但我必须使用一些输入法来输入中文。如果这是问题? (如果我使用中文输入法,JQuery无法检测到Enter)?
试试这个
< html xmlns =http://www.w3.org/1999/xhtml>
< head id =Head1>
< title>< / title>
将脚本类型= 文本/ JavaScript的 SRC = http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js >< /脚本>
< / head>
< body>
< div>
< input id =TestTextBoxtype =text/>
< / div>
< / body>
< script type =text / javascript>
$(function()
{
var testTextBox = $('#TestTextBox');
var code = null;
testTextBox.keypress(function(e)
{
code =(e.keyCode?e.keyCode:e.which);
if(code == 13)alert('Enter key was pressed。');
e.preventDefault();
});
});
< / script>
< / html>
Update:
I finally figured out that "keypress" has a better compatibility than "keydown" or "keyup" on Linux platform. I just changed "keyup"/"keydown" to "keypress", so all went well.
I don't know what the reason is but it is solution for me. Thanks all who had responsed my question.
--
I have some codes that needs to detect key press event (I have to know when the user press Enter) with JQuery and here are the codes in Javascript:
j.input.bind("keyup", function (l) {
if (document.selection) {
g._ieCacheSelection = document.selection.createRange()
}
}).bind("keydown", function(l) {
//console.log(l.keyCode);
if (l.keyCode == 13) {
if(l.ctrlKey) {
g.insertCursorPos("\n");
return true;
} else {
var k = d(this),
n = k.val();
if(k.attr('intervalTime')) {
//alert('can not send');
k.css('color','red').val('Dont send too many messages').attr('disabled','disabled').css('color','red');
setTimeout(function(){k.css('color','').val(n).attr('disabled','').focus()},1000);
return
}
if(g_debug_num[parseInt(h.buddyInfo.id)]==undefined) {
g_debug_num[parseInt(h.buddyInfo.id)]=1;
}
if (d.trim(n)) {
var m = {
to: h.buddyInfo.id,
from: h.myInfo.id,
//stype: "msg",
body: (g_debug_num[parseInt(h.buddyInfo.id)]++)+" : "+n,
timestamp: (new Date()).getTime()
};
//g.addHistory(m);
k.val("");
g.trigger("sendMessage", m);
l.preventDefault();
g.sendStatuses("");
k.attr('intervalTime',100);
setTimeout(function(){k.removeAttr('intervalTime')},1000);
return
}
return
}
}
It works fine on Windows but on Linux, it fails to catch the Enter event sometimes. Can someone help?
Updated:
It seems good if I only use English to talk. But I have to use some input method to input Chinese. If it is the problem? (JQuery can not detect Enter if I use Chinese input method? )
Try this
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<div>
<input id="TestTextBox" type="text" />
</div>
</body>
<script type="text/javascript">
$(function()
{
var testTextBox = $('#TestTextBox');
var code =null;
testTextBox.keypress(function(e)
{
code= (e.keyCode ? e.keyCode : e.which);
if (code == 13) alert('Enter key was pressed.');
e.preventDefault();
});
});
</script>
</html>
这篇关于在JavaScript中使用JQuery检测关键事件(输入)(在Linux平台上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!