问题描述
我需要实现两个目标,但我一次归档一个目标,而不是两个目标。
首先我有一个输入字段,当按下一个键时我应该触发一个事件,我需要捕获字段值。我使用字母,数字和TAB键。因此,如果我使用keyup,它会在第一个char处激活。如果我使用keydown,它需要2个字符才能触发,因为它在第一次触发时尚未按下char。因此,当我第二次按下时,它会以第一个字母开始,依此类推。
I need to archieve 2 objectives but I archive one at time, never both of them.First I have an input field that should fires an event when a key is pressed and I need to catch the field value. I use letters, number and the TAB key. So if I use keyup it fires at the first char. If I use keydown it takes 2 char to fire because when it fires the first time the char is not pressed yet. So when I press for the second time it fires with the first letter and so on.
据说,我需要的是关键事件放在在该字段中的值然后触发该事件。但是TAB在我的情况下有一个特殊的含义,它不是默认行为,并且使用TAB键我无法捕获e.which,e.charCode和e.keyCode!只有使用keydown,我才能获得这些价值!
Said that, it is clear that what I need is the keyup event that put the value in the field then the event is fired. But TAB has a special meaning in my case and it is not the default behavior and with TAB key I am unable to catch e.which, e.charCode nor e.keyCode! Only with keydown I am able to get those value!
现在我不知道该怎么办。我如何捕获TAB键或使keydown捕获字段的值?
Now I don´t have a clue what to do. How could I catch TAB key or make keydown catch the value of a field?
P.S keypress也作为keydown工作。在我获得该字段中的值之前触发事件
P.S keypress also working as keydown. Event is fired before I have the value in the field
编辑1:
以下是代码:
EDIT 1:Here is the code:
$('input[data-action="keyupNome"]').each(function () {
$(this).on("keypress", function(e) {
//Se o campo não estiver vazio
if($(this).val() != '') {
if(key != 9) // the tab key code
{
limpaCamposBusca('nome');
var width = $('#nomeBusca').width();
$('.nomeContainer').css('width', width);
$('.displayNomeTbl').css('width', width);
buscaEndereco('Controller/Dispatcher.php?classe=Buscas&acao=buscaEnderecoPorNome', 'nome');
}//if key == 9
else {
alert('here');
e.preventDefault();
}
}// val == ''
else {
clearFields();
clearBuscaCliente();
reactivateFields();
}
});
});
推荐答案
诀窍是使用 keydown
并将字段的实际值与当前按下的字符组合在一起以捕获 keydown
中的TAB并设置要在 keyup
在我的例子中。
The trick is to use keydown
and to combine actual value of the field with the char currently pressed OR to catch TAB in keydown
and set an external variable to be used in keyup
as in my example.
编辑:
事实上,我意识到不会阻止默认行为keydown中的TAB不会激活密钥。因此,不需要变量,但仅在keydown上阻止TAB。无论如何,如果你谈到的故障在某些情况下存在,这个版本总能工作。
EDIT :In fact, I realized that not preventing default behavior of TAB in keydown doesn't fire keyup. So, no variable is needed, but only preventing TAB on keydown. Anyhow, this version always work if the glitch you talked about exist in some circumstances.
(function() {
var tabKeyPressed = false;
$("#t").keydown(function(e) {
tabKeyPressed = e.keyCode == 9;
if (tabKeyPressed) {
e.preventDefault();
return;
}
});
$("#t").keyup(function(e) {
if (tabKeyPressed) {
$(this).val("TAB"); // Do stuff for TAB
e.preventDefault();
return;
}
//Do other stuff when not TAB
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="t" value="">
这篇关于使用键盘捕捉TAB键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!