简体   繁体   English

如何使用jQuery从两个复选框列表中获取选中复选框的数量?

[英]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>元素。

See this fiddle for a example. 有关示例,请参见此小提琴。

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.

相关问题 如何使用jQuery从两个表选中的复选框中获取值? - how to get from two table checked checkbox values using jquery? 如何获取使用jquery选中的复选框的值 - how to get the values of checkboxes that are checked using jquery 如何检查用户是否已使用jQuery / Javascript选中了一组复选框中的至少一个复选框? - How to check whether user has checked at least one checkbox from a group of checkboxes using jQuery/Javascript? 如何在分页 Jquery 数据表上检查复选框的数量 - How to get a count of checkboxes checked on pagination Jquery dataTables 如何使用jQuery从复选框组获取检查值 - how to get checked values from a checkbox-group using jquery 如何使用 jquery 限制选中复选框的数量等于 select 值? - How to limit count of checked checkboxes equal to select value using jquery? 如何使用jQuery获取每个选中的复选框 - How to get each checked checkbox using jQuery 如何获取页面上所有已选中复选框的计数 - How to get a count of all checked checkboxes on a page 如何使用 jQuery 动态获取复选框检查列值的总和 - How to get sum of checkboxes checked column values dynamically using jQuery 如何使用jQuery或JavaScript从一组选中的复选框中获取类名数组? - How to get an array of class-names from a set of checked checkboxes using jQuery or JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM