[英]Javascript on keypress + sign, how do I retrieve it
Javascript on keypress + sign, how do I retrieve it 按键+符号上的Javascript,如何检索它
Currently I got the following script for my enter key 目前,我的回车键有以下脚本
<script type="text/javascript">
$(document).ready(function() {
$(function(){
$("input[type=text]").keyup(function(e){
if(e.keyCode == "13")
{
var nextIndex = $(this).prop('tabindex') + 1;
var selectedId = $(this).prop('id');
var rowSplit = selectedId.split("_");
var rowIn = rowSplit[1];
if(selectedId.indexOf("big") > -1)
{
bigFunction(rowIn)
$('[tabindex=' + nextIndex + ']').focus();
}
$("form :input").on("keypress", function(e) {
return e.keyCode != 13;
});
});
</script>
I want it to work for if eg 我希望它能工作,例如
I got a text field with maxlength 4 我有一个最大长度为4的文本字段
I enter 4 random number eg 1234 and then I press the letter + sign, I want to change focus to next tabindex 我输入4个随机数字,例如1234,然后按字母+号,我想将焦点更改为下一个tabindex
How do I do it, thanks for helping so much !! 我该怎么做,谢谢你的帮助!
I might do this instead. 我可能会这样做。 Example only: 仅示例:
$('#element').keypress(function(){
var v = $(this).val();
if(v.charCodeAt(v.length-1) === 43){
// + was pressed
}
});
The key code is an integer, you wrote it properly the second time. 密钥代码是整数,您第二次正确编写了它。 But the first uses "13"
. 但是第一个使用"13"
。
if(e.keyCode == 13)
would probably work better. 可能会更好。 Also the +
sign code is 43 (0x2B). 同样, +
号代码是43(0x2B)。 In JavaScript you could also use the '+'.charCodeAt(0)
although of course that's much longer. 在JavaScript中,您也可以使用'+'.charCodeAt(0)
尽管这要长得多。
Also, when you hit enter, the default button is triggered unless you cancel the default behavior as in: 另外,当您按Enter键时,将触发默认按钮,除非您取消默认行为,如下所示:
e.preventDefault();
e.stopPropagation();
Canceling the default behavior for the +
would also help to avoid having the +
appear in the input. 取消+
的默认行为也将有助于避免在输入中出现+
。
To trigger the auto-tab feature when you have at least 4 characters (be it digits) you want to check the length of the input box value. 要在您至少有4个字符(为数字)时触发自动选项卡功能,您需要检查输入框值的长度。 Probably something like this with jQuery: 可能是这样的jQuery:
if($(this).val().length >= 4) ...
Otherwise it looks like you already got most of the code as required, although your code sample is currently missing a closing brace ( }
), so I'm not too sure I can help in fixing the code the way it is. 否则,尽管您的代码示例当前缺少右花括号( }
),但看起来好像您已经获得了大多数代码,因此我不太确定我可以按原样帮助修复代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.