【问题标题】:How to Auto Tab(Cursor) in Html.TextBoxFor(or Html.TextBox)?如何在 Html.TextBoxFor(或 Html.TextBox)中自动制表符(光标)?
【发布时间】:2013-07-18 14:21:14
【问题描述】:

我是 ASP.NET MVC 4 的新手,我有任何问题

这张图片(链接)后面有 5 个文本框。

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

在每个文本框中,我为其设置了 maxlength。关注这张图片(链接)

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

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

当我向每个文本框插入数据时,我想自动制表。

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

此后我想将数据设置为所有文本框

示例:字符串值 = textbox1 + textbox2 + ... + textbox5

        value = 1222233333...  

对于可能发生的任何错误,请提前接受我诚挚的歉意。

非常感谢。

【问题讨论】:

    标签: asp.net-mvc-4 html.textboxfor


    【解决方案1】:

    类似下面的东西应该可以工作,

    标记

    <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>
    

    脚本

    $(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);
            }
        });
    });
    

    小提琴:http://jsfiddle.net/tkasD/5/

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回答。所以我是 MVC 和 Javascript 的新手。我不知道如何声明 Javascript(您的代码:$(function () { ) 如何声明它。(在视图、控制器等中)
    • 查看stackoverflow.com/questions/10399122/… 在 mvc 应用程序中管理 js
    • 非常感谢你hhhhh!现在我可以解决这个问题。谢谢你帮助我。
    猜你喜欢
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2020-07-11
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多