0
在網絡表單中,我有一個複選框和3個文本框。我將所需的字段驗證器添加到所有3個文本框。我希望textbox1只有在複選框被選中時纔是必需的。我想用javascript來做。我正在使用customvalidator,我不知道我在哪裏做錯了。以下是我的代碼。使用javascript禁用asp驗證器
<form id="form1" runat="server">
<div>
<asp:CheckBox Checked="false" ID="chkBox" runat="server"/>
<asp:TextBox ID="txtBox1" Enabled="true" runat="server" />
<asp:CustomValidator id="newcstvalid" runat="server" ClientValidationFunction="CheckBoxValidation"></asp:CustomValidator>
<asp:RequiredFieldValidator runat="server" ID="rfvchkbox" ControlToValidate="txtBox1" ErrorMessage="error" ValidationGroup="abc"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="txtBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvtxt1" ControlToValidate="txtBox2" ErrorMessage="error" ValidationGroup="abc"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="txtBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="txtBox3" ErrorMessage="error" ValidationGroup="abc"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Button" CausesValidation="true" ValidationGroup="abc"/>
<asp:ValidationSummary runat="server" ID="valsumm" ValidationGroup="abc" ShowValidationErrors="true" />
</div>
</form>
我的javascript功能
<script type="text/javascript">
function CheckBoxValidation() {
var myCheckBox = document.getElementById("<%= chkBox.ClientID %>");
var validator = document.getElementById("<%= rfvchkbox.ClientID %>");
if (myCheckBox.checked)
{ ValidatorEnable(validator, true); }
else { ValidatorEnable(validator, false); }
}
</script>
感謝所有