3

我已经模拟了一个非常简单的页面,我非常困惑为什么当我在 2 个文本框之间切换时 javascript 不会触发:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" inherits="Checkout_test" %>

<!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">
<title>c</title>
<script type="text/javascript">
    function checkthisvalue(source, args) {
        alert('hello');
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Label ID="lbl1" AssociatedControlID="txtUsername" Text="1:" runat="server" />
    <asp:TextBox runat="server" CssClass="username" ID="txtUsername" ValidationGroup="vg"></asp:TextBox>
    <asp:CustomValidator Enabled="true" ID="cvUsername" Display="Dynamic" ValidationGroup="vg" ControlToValidate="txtUsername" EnableClientScript="true" ClientValidationFunction="checkthisvalue" ValidateEmptyText="true" runat="server">*</asp:CustomValidator>
    <asp:Label ID="lbl2" AssociatedControlID="txtPassword" Text="2:" runat="server" />
    <asp:TextBox runat="server" CssClass="username" ID="txtPassword"></asp:TextBox>
    <asp:CustomValidator Enabled="true" ID="cvPassword" Display="Dynamic" ControlToValidate="txtPassword" EnableClientScript="true" ClientValidationFunction="checkthisvalue" ValidateEmptyText="true" runat="server"></asp:CustomValidator>
</div>
</form>
</body>
</html>

请注意,两个 CustomValidators 都有 ValidateEmptyText=true

我已经在 IE 和 Chrome 中尝试过了,但没有成功。

4

1 回答 1

0

我想这不是空文本的问题。仅当CustomValidator您不更改控件值时,才会在选项卡导航时触发。(尝试在您的 中输入一个值TextBox,使用选项卡导航,返回清空TextBox,然后使用选项卡再次导航。CustomValidator 将触发)

考虑到这一点,让验证器触发一个简单的选项卡导航对用户来说会有些令人不安。

无论如何,验证器将在表单提交时触发(前提是您删除了first和ValidationGroup上的属性),并且值更改,这是他们应该做的。TextBoxValidator

希望这会有所帮助,

于 2013-04-17T13:58:37.700 回答