以下是checkbox
列表的代码:
数据以查找表为界限
<asp:CheckBoxList ID="cbl_AppliancePresent" ClientIDMode="Static"
CssClass="cbl_AppliancePresent"
runat="server" DataTextField="Appliance" DataValueField="ApplianceID" RepeatDirection="Horizontal">
</asp:CheckBoxList>
以下是Custom Validator
:
<asp:CustomValidator ID="SpecifyAppliances" ClientIDMode="Static"
runat="server" EnableClientScript="true" ErrorMessage="Please select
Vacant-not secure reasons" Font-Bold="True"
ClientValidationFunction="ValidateNotSecure" ForeColor="Red"
Font-Size="Small"></asp:CustomValidator>
功能:
function ValidateNotSecure(source, args) {
var options = $('.cbl_AppliancePresent');
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
EnableValidator('SpecifyAppliances');
args.IsValid = true;
return false;
}
}
DisableValidator('SpecifyAppliances');
args.IsValid = false; }
答案 0 :(得分:3)
如果你想通过简单地使用jquery并且不使用自定义验证器来解决它,那么这就是解决方案。
<body>
<form id="form1" runat="server">
<div id="chl1">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
<asp:ListItem>Item4</asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID="Label1" runat="server" Text="" ClientIDMode="Static"></asp:Label>
<asp:Button ID="btncheck" runat="server" Text="Button" ClientIDMode="Static" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$("#btncheck").click(function () {
var a = 0;
$(":checkbox").each(function () {
if (this.checked) {
a = a + 1;
}
});
if (a == 0) {
$("span[id$='Label1']").text('Please select');
return false;
}
});
});
</script>
</body>