`

jQuery: Get the checked and unchecked items from list of checkboxes

阅读更多

In this post we will see the use of jQuery in finding the count of checked and unchecked items from a list of checkboxes along with their count. This is also one of the common problems I faced and looked into how to use not selector and also found that count comes as not expected many a times so whats the reason behind that.

Let’s take a look at a generic example page where we have a list of checkboxes inside a table enclosed in a div (you can use any other scenario like a div having a list of checkbox elements only or a combination of checkboxes and other elements in it).

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.2.6.js" type="text/javascript"></script>
    <% if (false)
       { %>
    <script src="jquery-1.2.6-vsdoc.js" type="text/javascript"></script>
    <% }%>
    <script type="text/javascript">
        //Write your code when Document is loaded
        $(document).ready(function() {
            $("#testChk").click(function() {
                alert($("#testCheck :checked").size());
                //function to print the value of each checked checkboxes
                $("#testCheck :checked").each(function() {
                    alert("value = " + $(this).val());

            });

            $("#tblSub").click(function() {
                //show count of all not checked checkboxes only
                alert($("#testTB :input:not(:checked)").size());
                //show count of all not checked elements
                alert($("#testTB :not(:checked)").size());

                //function to print the value of each not checked checkboxes
                $("#testTB :input:not(:checked)").each(function() {
                    alert("value = " + $(this).val());
                });
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <div id="testCheck">
        <input type="checkbox" checked="checked" value="1" />
        <input type="checkbox" checked="checked" value="2" />
        <input type="checkbox" checked="checked" value="3" />
        <input type="checkbox" checked="checked" value="4" />
        <input type="checkbox" checked="checked" value="5" />
        <input type="checkbox" checked="checked" value="6" />
        <input type="checkbox" checked="checked" value="7" />
        <input type="checkbox" checked="checked" value="8" />
    </div>
    <input id="testChk" type="button" value="Submit" />

    <table id="testTB">
        <thead>
            <tr>
             <th>test</th>
             <th>chk boxes</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="1" /></td></tr>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="2" /></td></tr>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="3" /></td></tr>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="4" /></td></tr>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="5" /></td></tr>
            <tr><td>test</td><td><input type="checkbox" checked="checked" value="6" /></td></tr>
        </tbody>
    </table>
    <input id="tblSub" type="button" value="Submit" />
    </div>
    </form>
</body>
</html>
 

Since everything is present in UI so I am not giving the code behind as it would be default without any changes to verify how the above is working.

Here are few lines to take a look upon:
//show count of all not checked checkboxes only
1) alert($(“#testTB :input:not(:checked)”).size());
//show count of all not checked elements
2) alert($(“#testTB :not(:checked)”).size());

In the first one we get the count of all non checked checkboxes only because it uses an addition filter of ‘input’ tag type which is how checkboxes get rendered in html. In case we dont use it as in 2) example then we get all the elements inside div testTB which dont have checked attribute so it return all the tr, td etc elements along with input elements thereby increasing the count of return set.

This is a simple example but would help you in case you are trying to find the non cheched elements and getting unexpected count. The reason there would be that all the checkboxes are not inside one container and hence you would have to use the input tag type. Also you might have to consider addition filtering in case the same container contains other input elements as then not will filter those also in result set causing unexpected count. Som the gist is that either you do the wrapping in such a way that these checkboxes are clubbed together or else you need to use additional filter to find the right result set.

Hope this helps!

分享到:
评论

相关推荐

    Checked and Unchecked Exception

    本文将深入探讨“Checked”和“Unchecked”异常的区别,以及它们在实际编程中的应用。 Checked异常,也称为编译时异常,是那些在编译阶段必须被处理的异常。如果一个方法可能会抛出Checked异常,那么该方法要么通过...

    C++ and the Perils of Double-Checked Locking

    在介绍双检锁模式(Double-Checked Locking Pattern,DCLP)的C++实现中,Scott Meyers和Andrei Alexandrescu在其2004年的文章中指出,传统的单例模式实现并不具备线程安全性。单例模式是设计模式中经常被提及的一种...

    C++ and the Perils of Double Checked Locking.zip

    《C++ and the Perils of Double Checked Locking》是一篇探讨C++编程中双重检查锁定(Double-Checked Locking)模式潜在问题的文献。在多线程编程中,双重检查锁定是一种常见的优化策略,旨在减少对同步原语的依赖...

    jQuery中:checked选择器用法实例

    总结一下,`:checked`选择器在jQuery中是一个强大的工具,用于选取和操作用户在网页上选择的复选框和单选按钮。通过与其他选择器的组合,它可以实现复杂的交互逻辑,极大地提升了前端开发的效率和用户体验。在开发...

    jquery操作asp.net中GridView方法

    Get row and column index of a GridView Cell Playing with GridView Remove rows from GridView using jQuery Remove GridView columns using jQuery Drag and Drop GridView rows using jQuery Highlight...

    CListViewEx

    • If the LVS_EX_CHECKBOXES style has been applied and a selected item will be checked/unchecked, all other selected items will be checked/unchecked, too. • If the LVS_EX_LABELTIP style has been ...

    深入理解c# checked unchecked 关键字

    checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的;如果使用了checked发生数学运算溢出时会抛出OverflowException;如果使用了unchecked则不会检查溢出,算错了也不会报错。1. 一段编译没通过的代码...

    C#难点逐个击破(7):checked与unchecked

    在C#编程语言中,`checked`和`unchecked`关键字是用来控制整数算术运算和类型转换时是否进行溢出检查的重要工具。了解这两个关键字的用法和含义对于编写安全的代码至关重要,尤其是在处理可能涉及大数值计算或者类型...

    最新Ehlib 5.2.84(含完整源代码,支持delphi XE)

    + When the list of items for droped down filter list is formed, the current filter takes in account (Only when grid is conneccted to TMemTableEh). + Global variable DBGridEhShowInTitleFilterBox ...

    通过实例了解java checked和unchecked异常

    通过实例了解 Java checked 和 unchecked 异常 Java 异常分为两种类型:checked 异常和 unchecked 异常。checked 异常是可以在执行过程中恢复的,例如无效的用户输入、文件不存在、网络或者数据库链接错误等。这些...

    The Lancaster Corpus of Mandarin Chinese

    The Lancaster Corpus of Mandarin Chinese (LCMC) is designed as a Chinese match for the FLOB and FROWN corpora for modern British and American English. The corpus is suitable for use in both ...

    QCheckBox样式表(自定义)

    QCheckBox::indicator:checked QCheckBox 选中状态下的指示器。 QCheckBox::indicator:unchecked QCheckBox 未选中状态下的指示器。 QCheckBox::indicator:indeterminate QCheckBox 不定状态下的指示器(部分选中)...

    jquery attr方法获取input的checked属性问题

    在jQuery中,`attr`和`prop`方法都是用来处理元素属性的,但它们之间存在重要的区别,尤其是在处理像`checked`这样的布尔属性时。`attr`主要用于获取或设置HTML元素的特性(attributes),而`prop`则用于处理元素的...

    jquery 对checkbox的操作

    // log the value of each checked checkbox }); }); ``` 综上所述,jQuery为开发者提供了便利的API来处理checkbox,使得在网页中实现与用户交互变得更加简单和直观。无论是单个checkbox的控制还是批量操作,...

    详解Java中的checked异常和unchecked异常区别

    Java中的checked异常和unchecked异常区别详解 Java中的checked异常和unchecked异常是Java语言中两种不同的异常类型,它们之间的区别是很多开发者容易混淆的。下面,我们将详细介绍checked异常和unchecked异常的概念...

    C#的ListView在VirtualMode模式下带CheckBoxes的高性能操作

    但是,在VirtualMode模式下,CheckBoxes的实现却不是那么简单,不能通过设定CheckBoxes = true来实现,还要有特别的处理才能显示选择框. 最近需要用到带CheckBoxes的VirtualMode模式的ListView控件,通过查找资料和研究,...

    浅析jquery与checkbox的checked属性的问题

    在网页开发中,jQuery 是一个广泛使用的 JavaScript 库,它简化了 DOM 操作,包括对复选框(checkbox)的处理。本文将深入探讨 jQuery 与 HTML 复选框的 `checked` 属性之间可能遇到的问题及其解决方案。 1. **页面...

Global site tag (gtag.js) - Google Analytics