开发者

how to auto press "tab" key when user press "-" (dash) key in javascript

开发者 https://www.devze.com 2023-01-26 17:59 出处:网络
my code like this function Tab(e) { var input = e.keyCode ? e.keyCode : e.charCode; if ((input>=48 && input<=57) 开发者_开发技巧|| (input==45))

my code like this

        function Tab(e)
        {
            var input = e.keyCode ? e.keyCode : e.charCode;
            if ((input>=48 && input<=57) 开发者_开发技巧|| (input==45))
            {
                if (input==45)
                {
                //what should i do here? so,  
                //if user press "-", its auto tab to next textfield,
                    return false
                }
                else
                return true;
            }
            else 
            return false;
        }

here my html

<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)">
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)">
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event)">

I've been searching at google. but its return similar article and it's not that I'm looking for.

I have a lot of similar text field, so it is not possible to include the next textfield's name cause of i used array name.

sorry if my english is bad, but i hope you understand what I want


You can focus the next input sibling in this way:

HTML:

<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)">
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)">
<input type="text" name="a[]" maxlength='3' onkeypress="return Tab(event, this)">

JS:

    function Tab(e, inp)
    {
        var input = e.keyCode ? e.keyCode : e.charCode;
        if ((input>=48 && input<=57) || (input==45))
        {
            if (input==45)
            {
                //focus the next input if there is one
                while(inp.nextSibling)
                {
                   var inp=inp.nextSibling;
                   if(inp.nodeType===1 && inp.tagName.toLowerCase()=="input")
                   {
                        inp.focus();
                        break;
                   }
                }
                return false
            }
            else
            return true;
        }
        else 
        return false;
    }


it just takes 10 seconds to ask google. you can't emulate a tab-keypress, but there are different workarounds (maybe you could use an array containing the id's of all you fields, save the index you're at and on pressing dash, focus index+1 in your array (+ setting the saved index onfocus of every text field to note if a user focuses a field by clicking it or really pressing tab))


I've had a similar problem, where I wanted to press + on the numpad to tab to the next field. Now I've released a library that I think will help you. It does require jquery though.

PlusAsTab: A jQuery plugin to use the numpad plus key as a tab key equivalent.

Since you want - (on the normal keys, I guess) instead, you can set the options. Find out which key you want to use with the jQuery event.which demo.

JoelPurra.PlusAsTab.setOptions({
  // Use dash instead of plus
  // Number 189 found through demo at
  // https://api.jquery.com/event.which/
  key: 189
});

// Matches all inputs with name "a[]" (needs some character escaping)
$('input[name=a\\[\\]]').plusAsTab();

You can try it out in the PlusAsTab demo, and check out the enter-as-tab demo. If you want to change to the - key, you can call JoelPurra.PlusAsTab.setOptions({key: 189}); from your javascript console.

0

精彩评论

暂无评论...
验证码 换一张
取 消