Microsoft JScript运行时错误:需要对象

Gok*_*kul 0 javascript

我写了一个简单的javascript函数来验证aspx页面中的字段

function validate()

{
 if (document.getElementById("<%=tbName.ClientID%>").value=="")
  {
             alert("Name Feild can not be blank");
             document.getElementById("<%=tbName.ClientID%>").focus();
             return false;
  }
  if (document.getElementById("<%=ddlBranch.ClientID%>").value=="SelectBranch")
  {
             alert("Branch Should Be Selected");
             document.getElementById("<%=ddlBranch.ClientID%>").focus();
             return false;
  }

}
Run Code Online (Sandbox Code Playgroud)

一切都很好.后来我将它链接到aspx页面,就像一个外部的js文件.

head runat ="server">

script src ="validation.js"type ="text/javascript">

/ SCRIPT>

<title>Validations</title>
Run Code Online (Sandbox Code Playgroud)

/ HEAD>

<form id="form" runat="server">



<asp:Label ID="lblName" runat="server" Text="Nmae: ">/asp:Label>
<asp:TextBox ID="tbName" runat="server" Width="130px">/asp:TextBox>

<asp:Label ID="lblBranch" runat="server" Text="Branch:">/asp:Label>
    <asp:DropDownList ID="ddlBranch" runat="server" Width="107px">
        <asp:ListItem>CSE</asp:ListItem>
        <asp:ListItem>ECE</asp:ListItem>
        <asp:ListItem>CIVIL</asp:ListItem>
        <asp:ListItem>MECH</asp:ListItem>
        <asp:ListItem Selected="True" Value="SelectBranch">SelectBranch</asp:ListItem>
    </asp:DropDownList>    
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return validate(<%=tbName.ClientID%>, <%=ddlBranch.ClientID%>)" />

</form> 
Run Code Online (Sandbox Code Playgroud)

在aspx.cs页面中

protected void Page_Load(object sender,EventArgs e)

{
    Page.RegisterClientScriptBlock("MyScript", "<SCRIPT Language='JavaScript' src='validation.js'></SCRIPT>"); 
    btnSubmit.Attributes.Add("onclick", "return validate()");

}
Run Code Online (Sandbox Code Playgroud)

现在它给出错误"Microsoft JScript运行时错误:需要对象".

我无法知道我哪里出错了.

Nic*_*ver 7

您的脚本只能 aspx页面运行.<%=tbName.ClientID%>是服务器端逻辑,它将文本客户端ID放在输出到客户端,所以在HTML中呈现之前看起来像这样:

document.getElementById("tbName")
//looks for <input id="tbName" />
Run Code Online (Sandbox Code Playgroud)

现在看来只是这样的:

document.getElementById("<%=tbName.ClientID%>")
//looks for <input id="<%=tbName.ClientID%>" /> ... doesn't exist :)
Run Code Online (Sandbox Code Playgroud)

由于它不再找到对象/元素(因为 ID不存在),您将收到object required错误.您必须在页面中保留此逻辑,或者使用类等转移到其他方法.如果您正在进行大量验证,我将查看jQuery验证库.


更新:这是TJ为您提供的全文形式的解决方案,以便于阅读.如果您只验证几个字段,这是最简单的解决方案:

function validate(nameId, branchId) {
  if (document.getElementById(nameId).value=="")
  {
             alert("Name Feild can not be blank");
             document.getElementById(nameId).focus();
             return false;
  }
  if (document.getElementById(branchId).value=="SelectBranch")
  {
             alert("Branch Should Be Selected");
             document.getElementById(branchId).focus();
             return false;
  }
}
Run Code Online (Sandbox Code Playgroud)

在您的代码隐藏中:

//Note your current method is deprecated after .Net 2.0+, should use this instead:
//ClientScript.RegisterClientScriptInclude("validation", "validation.js");
Page.RegisterClientScriptBlock("MyScript", "<script type='text/javascript' src='validation.js'></script>"); 
btnSubmit.Attributes.Add("onclick", string.Format("return validate('{0}','{1}')", tbName.ClientID, ddlBranch.ClientID));
Run Code Online (Sandbox Code Playgroud)