How to Auto Tab(Cursor) in Html.TextBoxFor(or Html

2019-06-02 23:37发布

问题:

I'm new in ASP.NET MVC 4, and I have any questions

I have 5 Textbox following This Picture(Link).

http://i.stack.imgur.com/hMjJp.png

in each textbox I set maxlength for its. Following This Picture(Link)

http://i.stack.imgur.com/rSi4U.png

Example : textbox1 -> maxlength = 1
          textbox2 -> maxlength = 4
          textbox3 -> maxlength = 5

I want to auto tab when i insert data to each textbox.

Example : when I insert "1" to textbox1(maxlength=1) cursor will go to textbox2 AUTO

And thereafter I want to set data as All textbox

Example : string value = textbox1 + textbox2 + ... + textbox5

        value = 1222233333...  

Please accept my sincere apology in advance for any mistake that may occur.

Thank You Very Much.

回答1:

Something like following should work,

markup

<div class="tax">
    <input type="text" maxlength="1" />
    <input type="text" maxlength="4" />
    <input type="text" maxlength="5" />
    <input type="text" maxlength="2" />
    <input type="text" maxlength="1" />
</div>

script

$(function () {
    $('.tax input[type="text"]').keypress(function (e) {
        if (e.which == 0 || e.charCode == 0) {
            return true;
        }
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        str = $(this).val() + str;

        if (str.length == $(this).prop('maxlength')) {
            var that = this;
            window.setTimeout(function(){
                $(that).next('input[type="text"]').focus();
            }, 0);
        }
    });
});

fiddle: http://jsfiddle.net/tkasD/5/

hope this helps.