![](/img/trans.png)
[英]JavaScript runtime error: Unable to get property 'value' of undefined or null reference
[英]JavaScript runtime error Unable to get property 'value' of undefined or null reference
我无法访问JS文件中的变量。
当在同一页面中编写时,验证工作正常。 但是,当我将验证代码粘贴到单独的JS文件中时,它不起作用。
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/NewValidation.js" type="text/javascript"></script>
<table id="tblUpdateReg">
<thead>
<tr>
<td>
Update Details
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
Username :
</div>
</td>
<td>
<input type="text" runat="server" id="txtUsername" />
</td>
</tr>
</tbody>
</table>
</asp:Content>
JS档案:
function Validate() {
var username= document.getElementById("txtUsername").value;
if (username== "") {
alert("username Required.");
return false;
}
return true;
}
错误:JavaScript运行时错误:无法获取未定义或空引用的属性“值”
这意味着ID为txtUsername的元素不存在。 您在创建元素之前执行脚本。
您应该将脚本放在页面底部,或者使用
window.onload = function() {}
在其中执行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.