判断Repeater 下嵌套绑定的CheckBoxList和RadioButtonList控件是否选中(答题)


js验证Repeater 下嵌套绑定的CheckBoxList和RadioButtonList控件每组中是否有至少1个被选中。

CheckBoxList默认显示成表格结构 设置RepeatLayout="UnorderedList"让其生成的代码为如下结构。

 <dd>
<span id="ContentPlaceHolder1_repList_RadList_1">
<input id="ContentPlaceHolder1_repList_RadList_1_0_1" type="radio" name="ctl00$ContentPlaceHolder1$repList$ctl02$RadList" value="A" />
<label for="ContentPlaceHolder1_repList_RadList_1_0_1">111</label>
<input id="ContentPlaceHolder1_repList_RadList_1_1_1" type="radio" name="ctl00$ContentPlaceHolder1$repList$ctl02$RadList" value="B" />
<label for="ContentPlaceHolder1_repList_RadList_1_1_1">222</label>
<input id="ContentPlaceHolder1_repList_RadList_1_2_1" type="radio" name="ctl00$ContentPlaceHolder1$repList$ctl02$RadList" value="C" />
<label for="ContentPlaceHolder1_repList_RadList_1_2_1">333</label>
</span>
</dd>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatLayout="UnorderedList">
</asp:CheckBoxList>
<script type="text/javascript">
$(document).ready(function () {
$("#form1").submit(function () {
var counts = 0;
$("dd span").each(function () {
var fload = false;
var nodes = this.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].type == "radio"||nodes[i].type=="checkbox") {
if (nodes[i].checked) {
fload = true;
}
}
}
if (fload) {
counts++;
}
});
counts = $("dd").length - counts;
if (counts > 0) {
alert("您有" + counts + "题未答!");
return false;
}
});
});
</script>




原文地址:https://www.cnblogs.com/cniteeq/p/2292707.html