试试下面的代码:
解决方案-1
每当您选中复选框时,警报都会显示复选框中选中项目的值。
Javascript代码:
<脚本类型="文本/javascript">
function itemClick(radioButton) {
var Control;
var selectedItems;
selectedItems = '';
Control = document.getElementById("<%=ckbList2DesignationUc.ClientID %>").getElementsByTagName("input");
for (var i = 0; i < Control.length; i++) {
if (Control[i].checked == true)
selectedItems += i.toString()+',';
}
alert(selectedItems);
}
</script>
ASPX 代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int index = 0; index < 10; index++)
{
ckbList2DesignationUc.Items.Add(new ListItem("Item" + index, index.ToString()));
}
foreach (ListItem Li in ckbList2DesignationUc.Items)
{
Li.Attributes.Add("onclick", "return itemClick()");
}
}
使用 Jquery 所需的解决方案:
解决方案-2
< script type="text/javascript">
$(document).ready(function () {
var selectedCheckBoxes = '';
$('#ckbList2DesignationUc').each(function () {
var items = new Array();
$(this).find("input:checkbox").each(function () {
$(this).click(function () {
selectedCheckBoxes += $(this).val() + ",";
alert(selectedCheckBoxes);
});
});
});
});
< /script>
< asp:CheckBoxList ID="ckbList2DesignationUc" runat="Server" RepeatLayout="Flow" RepeatDirection="Horizontal" Width="100%">
< /asp:CheckBoxList>