文本框在javascript中为null或未定义

时间:2017-08-31 16:51:22

标签: javascript asp.net

我正在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作为参数传递。这会对文本框返回为空的原因产生影响吗?

1 个答案:

答案 0 :(得分:0)

我仍然不能100%确定以上为什么不起作用,但这是我的修复

<div id="input-upload-file">
  <p>
  Video Upload Duration test
  </p>
  <input id="videoInput" type="file" class="upload" name="fileUpload">
</div>

我取出参数并使其静止