【问题标题】:Textbox is a null or undefined in javascript文本框在 javascript 中为 null 或未定义
【发布时间】:2017-09-02 02:17:46
【问题描述】:

我正在 JavaScript 中为我的 aspx 页面中的文本框创建一个自动选项卡功能。当我尝试专注于下一个文本框时,我收到一条错误消息 "Unable to get property 'focus' of undefined or null reference".

   <asp:TextBox type="text" ID="areaCodeTextBox" runat="server" MaxLength="3" size="1" onkeypress="return isNumberKey(event)" onkeyup="Tab(this, 'exchangeTextBox')"  />
   <asp:TextBox type="text" ID="exchangeTextBox" runat="server" MaxLength="3" size="1" onkeypress="return isNumberKey(event)" onkeyup="Tab(this, 'suffixTextBox')"/>
   <asp:TextBox type="text" ID="suffixTextBox" runat="server" MaxLength="4" size="2" onkeypress="return isNumberKey(event)" />

   <SCRIPT language=Javascript>
  function isNumberKey(evt)
  {
     var charCode = (evt.which) ? evt.which : evt.keyCode;
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;    
     return true;
  }
  function Tab(fromTextBox, toTextBox) {
      // Determine if the current field's max length has been reached.
      var length = fromTextBox.value.length;
      var maxLength = fromTextBox.getAttribute("maxLength");
      if (length == maxLength) {
          // Retreive the next field in the tab sequence, and give it the focus.

          document.getElementById(toTextBox).focus();
      }
  }
 </SCRIPT>

我调用焦点方法的方式有什么明显的错误吗?

编辑* 我没有直接在 javascript 中引用文本框,我试图将 ID 作为参数传递。这会对为什么文本框返回 null 有影响吗?

【问题讨论】:

标签: javascript asp.net


【解决方案1】:

我仍然不能 100% 确定为什么上述方法不起作用,但这是我的解决方法

<script>
// A $( document ).ready() block.
$(document).ready(function () {
    //Attach key up event handler
    $('#<%= areaCodeTextBox.ClientID %>').keyup(function () {
                //check if user typed three characters
                if (this.value.length == $(this).attr('maxlength')) {
                    //move the focus to another textbox
                    $('#<%= exchangeTextBox.ClientID %>').focus();
                }
            });
            $('#<%= exchangeTextBox.ClientID %>').keyup(function () {
                if (this.value.length == $(this).attr('maxlength')) {
                    $('#<%= suffixTextBox.ClientID %>').focus();
               }
            });
        });
</script>

我把参数拿出来做成静态的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多