如何将光标从textbox1定位到ASP.Net中的textbox2?

时间:2018-02-03 05:14:45

标签: c# jquery asp.net

我的网页应用中有两个textboxes用于登录页面,RegNoAddress。现在我希望如果页面加载,那么光标应该在RegNo文本框上。输入RegNo后,必须在按Address后转到Enter。怎么做?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   </head>
<body bgcolor="#CCCCCC"  >
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="RegNo" runat="server" Text=""></asp:TextBox>
        <asp:TextBox ID="Name" runat="server" Text=""></asp:TextBox>
        <asp:TextBox ID="Address" runat="server" Text=""></asp:TextBox>
        <asp:Button  CssClass="sCancel" ID="btnCancel" runat="server" Text="Close"  onclick="btnCancel_Click" Width="84px"  /> 
        <asp:Button CssClass="sUpdate" ID="btnUpdate" runat="server" Text="Update"  onclick="btnUpdate_Click" Width="84px" style="margin-left: 0px"/> 
        <asp:Button CssClass="sAdd" ID="Add" runat="server" Text="Add" Width="84px"  onclick="Add_Click"   />  

    </div>
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

好吧,我假设你问的是如何将焦点从userid更改为password,而你不需要自己移动光标。

请注意,无需在服务器端中执行此操作。您可以使用 JQuery 客户端中简单地执行此操作。如果您不熟悉 JQuery ,请参阅here以了解如何将 JQuery 添加到客户端代码中。

现在我假设客户端的代码是这样的:

Username: <asp:TextBox name="userid" id="userid" /><br>
Password: <asp:TextBox name="password" id="password" />

我将为此提供两个JQuery示例,在第一个示例中,当用户按 Enter 时,焦点将跳转到密码

$('#userid').keydown(function (e) {
    if (e.keyCode == 13) {
        $("#password").focus();
        return false;
    }
});

在下一个示例中,当 userid 达到特定长度时,我们将焦点更改为密码。例如6:

$('#userid').keydown(function (e) {
    if ($("#userid").val().length > 5) {
        $("#password").focus();
    }
});

<强>更新

它应该适用于您的代码:

<head runat="server">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>

        $( document ).ready(function() {
            $('#RegNo').keydown(function (e) {
                if (e.keyCode == 13) {
                    $("#Name").focus();
                    return false;
                }
             });
        });

    </script>

</head>
<body bgcolor="#CCCCCC">
    <form id="form1" runat="server">
        <div>

            <asp:TextBox ID="RegNo" runat="server" Text=""></asp:TextBox>
            <asp:TextBox ID="Name" runat="server" Text=""></asp:TextBox>

        </div>
     </form>
</body>

答案 1 :(得分:0)

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>

        $(document).ready(function () {
            $('#RegNo').keydown(function (e) {
                if (e.keyCode == 13) {
                    $("#Name").focus();
                    return false;
                }
            });
            $('#Name').keydown(function (e) {
                if (e.keyCode == 13) {
                    $("#Address").focus();
                    return false;
                }
            });
            $('#Address').keydown(function (e) {
                if (e.keyCode == 13) {
                    $("#Add").focus();
                    return false;
                }
            });


        });

    </script>

答案 2 :(得分:-1)

您可以通过使用TaxtBox的Tabindex属性来实现此目的。

首先,将Tabindex = 1设置为Textbox1 然后,将Tabindex = 2设置为TextBox2,依此类推..

当你的页面加载时,它会显示控制的tabindex,而不是那么关注它。

由于