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

萝らか妹 提交于 2019-12-08 05:59:32

问题


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.



来源:https://stackoverflow.com/questions/17713707/how-to-auto-tabcursor-in-html-textboxforor-html-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!