javascript 如何使用jquery从两个复选框列表中获取选中复选框的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13488952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get count of checked checkboxes from two checkbox lists using jquery?
提问by Ram
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.
我已经尝试过使用类,但该类被附加到复选框列表的 html 中的表格渲染中。
回答by Cerbrus
var n = $("input:checked").length;
Assuming the asp page returns a bunch of <input>
elements.
假设asp页面返回一堆<input>
元素。
回答by Anshuman Jasrotia
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”的复选框列表检查的所有复选框的计数
回答by Adil
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;
回答by polin
<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>