简体   繁体   English

按键+符号上的Javascript,如何检索它

[英]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.

相关问题 我如何在 javascript 的按键上打印数字 - How do i make a number print on keypress on javascript javascript 如何使用按键事件填充元素 - javascript How do I fill an element using keypress event 如何在JavaScript中检索变量的名称 - How do I retrieve the name of a variable in JavaScript 在按键事件中,如何将&#39;,&#39;更改为&#39;〜&#39; - On keypress event, how do I change a ',' to a '~' 如何在Backbone中绑定按键? - How do I bind keypress up in Backbone? 如何在 javascript(开玩笑测试)中调度多个调度按键事件? - How do I dispatch multiple dispatch keypress events in javascript(jest testing)? 如何在键盘上输入“ Enter”键 <input> 元素转移到下一个焦点 <input> 页面上的元素。 (JavaScript的) - How do I make “Enter” keypress in an <input> Element shift focus to the next <input> element on the page. (JavaScript) 如何停止在JavaScript中触发onAuthStateChange的“注册”? - How do i stop 'sign up' from triggering onAuthStateChange in javascript? 我如何使用#登录javascript比较对象 - How do i compare objects using # sign in javascript 如何使用JavaScript字符串替换方法替换“ +”号? - How do I use the JavaScript string replace method to replace the '+' sign?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM