[英]how to get count of checked checkboxes from two checkbox lists using jquery?
I have two checkbox lists, i want to get the count of checked elements of these. 我有两个复选框列表,我想获取这些复选框的计数。 below is my code:
下面是我的代码:
<div id="divAreaListingByLocation">
<asp:CheckBoxList ID="chklstArea" CssClass="chkarea" RepeatColumns="6" RepeatDirection="Vertical"
runat="server" OnSelectedIndexChanged="chklstArea_SelectedIndexChanged" AutoPostBack="true">
</asp:CheckBoxList>
</div>
<asp:Repeater ID="repRooms" runat="server" OnItemDataBound="repRooms_ItemDataBound">
<ItemTemplate>
<div style="height: 100%; float: none;">
<asp:Panel ID="pnlRoomheader" runat="server" Style="width: 98%; background-color: #86ADD6;
color: #4A4A4A; height: 20px; margin-top: 10px; text-align: left; padding: 5px;
font-size: 15px; font-weight: bold;">
<asp:Label ID="lblAreaName" runat="server"></asp:Label>
<asp:Label ID="lblAreaId" Style="display: none;" runat="server"></asp:Label>
</asp:Panel>
<div id="divRoomListingByLocation" style="padding-bottom: 10px; padding-top: 10px;">
<asp:CheckBoxList ID="chkRoomList" CssClass="chkRooms" RepeatColumns="6" RepeatDirection="Vertical"
runat="server">
</asp:CheckBoxList>
<asp:Label ID="lblRoomMessage" Text="This Area does not have any room." ForeColor="Red"
runat="server"></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
What i want to do is: if user not checked any of the check box from these two then it will prompt a alert to say check one of the checkbox from both lists on the click of a button. 我想要做的是:如果用户未选中这两个复选框中的任何一个,则它会提示您单击按钮以选中两个列表中的一个复选框。
I have tried to it with class but the class is append to the table render in html of checkboxlist. 我已经尝试过使用类,但是该类被追加到checkboxlist的html表格渲染中。
With jQuery : 使用jQuery :
var n = $("input:checked").length;
Assuming the asp page returns a bunch of <input>
elements. 假设asp页返回一堆
<input>
元素。
Since you are using checkbox list it will apply the specified class to the Table not the checkbox. 由于您正在使用复选框列表,因此会将指定的类应用于表而不是复选框。 There you will have to use
在那里你将不得不使用
$(".chkarea").find("input:checked").length;
This will return the count of all the checkboxes that are checked for a checkboxlist with class "chkarea" 这将返回所有已检查类别为“ chkarea”的复选框列表的复选框的计数
You can use wild card for checkbox list id as the ids generated by checkbox list will start by this id. 您可以对复选框列表ID使用通配符,因为复选框列表生成的ID将从该ID开头。
count = $("[id^=chklstArea] , [id^=chkRoomList]").filter(function(){
if($(this).is(':checked'))
return $(this);
}).length;
<script type="text/javascript">
function fnc()
{
x=document.getElementById("chkdiv").elements;
for(i=0;i<x.length;++i)
{if(x[i].type=='checkbox' && x[i].checked)
alert("checked");
}
}
</script>
<form id="chkdiv">
<input type="checkbox" id="chk1">
<input type="checkbox" id="chk2">
<button id="button" onClick="fnc()">click</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.