1

为什么它不起作用?

如果用户在下拉列表中选择“程序员”并在文本框中输入“12”,我希望触发验证。但是当我单击提交按钮时什么都没有发生。

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <script language="JavaScript">
   var text = document.getElementById("Textbox2");
   function Date(oSrc, args) {
   args.IsValid = (args.Value == "Programmer" && text == "12");
   }
   </script>

   <title></title>
   </head>
   <body>
   <form id="form1" runat="server">
   <div>

   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
   <asp:Button ID="Button1" runat="server" Text="Button" />
   <asp:CustomValidator ID="dateValidator" runat="server" 
   ClientValidationFunction="Date" ControlToValidate="DropDownList1" Display="Dynamic" 
   ErrorMessage="Sample error!"></asp:CustomValidator>

   <asp:DropDownList ID="DropDownList1" runat="server">
  <asp:ListItem Selected="True">Select a profession</asp:ListItem>
  <asp:ListItem>Programmer</asp:ListItem>
  <asp:ListItem>Lawyer</asp:ListItem>
  <asp:ListItem>Doctor</asp:ListItem>
  <asp:ListItem>Artist</asp:ListItem>
   </asp:DropDownList>

   </div>
   </form>
   </body>
   </html>
4

1 回答 1

2

试试下面

<script language="JavaScript">
   function Date(oSrc, args) {
   args.IsValid = (args.Value == "Programmer" && document.getElementById('<%=TextBox2.ClientID%>').value== "12");
}
于 2013-08-24T14:30:37.607 回答