【问题标题】:how to get count of checked checkboxes from two checkbox lists using jquery?如何使用jquery从两个复选框列表中获取选中复选框的数量?
【发布时间】:2012-11-09 10:47:43
【问题描述】:

我有两个复选框列表,我想获取其中选中元素的计数。以下是我的代码:

<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>

我想要做的是:如果用户没有选中这两个复选框中的任何一个,那么它会提示一个警报,说在单击一个按钮时从两个列表中选中一个复选框。

我已经尝试过使用类,但该类被附加到复选框列表的 html 中的表格渲染中。

【问题讨论】:

  • 你有没有尝试过?
  • 请发布您尝试过的内容。类似的问题已经被问过:stackoverflow.com/questions/872296/….
  • @FelixKling 你提到的问题与我在那个问题中的问题不同,他问的是如何计算一个页面的所有复选框的数量..以及我的复选框列表中的问题..
  • @Visions:嗯,当然你必须调整选择器......如果这是你的问题(不清楚,因为你没有发布你尝试过的内容),我建议你看看在文档中:api.jquery.com/category/selectors。查看生成的 HTML,而不是 asp 代码也可能会有所帮助。

标签: javascript jquery asp.net checkbox


【解决方案1】:

由于您使用的是复选框列表,它会将指定的类应用于表格而不是复选框。在那里你将不得不使用

$(".chkarea").find("input:checked").length;

这将返回为“chkarea”类的复选框列表选中的所有复选框的计数

【讨论】:

    【解决方案2】:

    With jQuery:

    var n = $("input:checked").length;
    

    假设asp页面返回一堆&lt;input&gt;元素。

    See this fiddle for a example.

    【讨论】:

    • 它用于返回页面的所有选中复选框的计数,我的问题是我想获取复选框列表的选中复选框的计数............
    • 它实际上只计算选中的复选框。示例:jsfiddle.net/9na5Q/1
    【解决方案3】:
        <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>
    

    【讨论】:

      【解决方案4】:

      您可以使用通配符作为复选框列表 id,因为复选框列表生成的 id 将以此 id 开头。

      count = $("[id^=chklstArea] , [id^=chkRoomList]").filter(function(){
                  if($(this).is(':checked')) 
                        return $(this);
               }).length;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-22
        • 2011-01-10
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多