使用CustomValidator对asp.net Single-CheckBox进行客户端验证的JavaScript [英] JavaScript for Clientside validation for asp.net Single-CheckBox using CustomValidator
问题描述
我正在尝试使用以下代码验证该复选框.
但是它总是返回false.
//----------------------------------------
I am trying to validate the check box using the following code.
But it is always return false.
//----------------------------------------
<script language="javascript" type ="text/javascript" >
function checkAgreement(source, args)
{
var elem = document.getElementById("cbxPDCheque");
if (elem.checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
//================================================ =====
//=====================================================
<asp:CheckBox ID="cbxPDCheque" runat="server" AutoPostBack="True" Text="PD Cheques" />
<asp:CustomValidator ID="CustomValidator1" runat="server" EnableClientScript ="true"
ErrorMessage="Select PD Cheques." ClientValidationFunction ="checkAgreement" >*</asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False" />
<asp:Button ID="Button1" runat="server" Text="Button" />
//================================================ =======
//=======================================================
推荐答案
我认为您的ClientID错误.
I reckon your ClientID is wrong.
var elem = document.getElementById("cbxPDCheque");
您需要查看页面的源代码以找出CheckBox的正确ClientID.服务器控件的ID不必与在浏览器中呈现的DOM属性ID相同.
You need to view the source of your page to find out the correct ClientID of the CheckBox. The ID of a server Control is not necessarily the same as the DOM attribute ID that gets rendered in the browser.
var elem = document.getElementById("<%=cbxPDCheque.ClientID%>");
因为控件的ID在渲染后已更改.
Because the ID of the controls is changed after rendering.
这篇关于使用CustomValidator对asp.net Single-CheckBox进行客户端验证的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!