使用属性TextMode =“MultiLine”关注文本框以输入按键

时间:2014-04-17 06:40:49

标签: c# javascript asp.net

在ASP网站中,我想使用输入键在文本框之间导航,这样当我按下回车键时,焦点应转到下一个文本框。

我所做的是在asptextbox中使用onkeypress函数,如下所示

<asp:TextBox ID="Textbox1" runat="server" onkeypress="return EnterEvent(event);"></asp:TextBox>

在javascript中     

          function EnterEvent(e) {

              if (e.keyCode == 13) {
                  document.getElementById("Textbox2").focus();
                  }
              }          
</script>

工作正常。但是,如果Textbox2是一个多行文本框(如果我使用TextMode =&#34; MultiLine&#34;对于一个asp文本框),那么焦点将放在Textbox2上,但它将在文本框中放置一个额外的行(焦点放在2多行文本框的行。如何解决?

1 个答案:

答案 0 :(得分:1)

尝试使用此脚本通过控件输入键导航..

<script type = "text/javascript" >
    function pageLoad(sender, args) {
        var inputs = $(':input,select').keydown(function (e) {

            if (e.which == 13) {
                if (e.target.id == '') {
                    return;
                }
                e.preventDefault();
                var nextInput = inputs.get(inputs.index(this) + 1);
                if (nextInput) {
                    nextInput.focus();
                    nextInput.select();
                }

            }
        });
    }
</script>

如果上述脚本不适合您的情况,您可以尝试将onkeypress更改为onkeydown ..您可以使用任何一种情况,而不是同时使用.. < / p>

<asp:TextBox ID="Textbox1" runat="server" onkeydown="return EnterEvent(event);"></asp:TextBox>

您可以通过参考以下链接了解keydown和keypress之间的区别。

Difference between Keypress and keydown...