我有两个以上的复选框列表。我想限制用户为每个复选框列表仅选择 2 个项目。
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="Item 1">Item 1</asp:ListItem>
<asp:ListItem Value="Item 2">Item 2</asp:ListItem>
<asp:ListItem Value="Item 3">Item 3</asp:ListItem>
<asp:ListItem Value="Item 4">Item 4</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBoxList ID="CheckBoxList2" runat="server">
<asp:ListItem Value="Item 1">Item 1</asp:ListItem>
<asp:ListItem Value="Item 2">Item 2</asp:ListItem>
<asp:ListItem Value="Item 3">Item 3</asp:ListItem>
<asp:ListItem Value="Item 4">Item 4</asp:ListItem>
</asp:CheckBoxList>
使用 javascript/jquery。请帮忙
解决方案:
<script type="text/javascript">
var i = 1;
$(document).ready(function () {
$('table[id^=CheckBoxList]').each(function (index) {
var id = '#' + this.id;
$(id).click(function (e) {
if ($(id).children().find('input[type="checkbox"]:checked').length > 2) {
e.preventDefault();
}
});
i++;
});
});
</script>
谢谢大家:)