Home >Web Front-end >JS Tutorial >Operations and precautions such as obtaining checkbox selected items in jQuery_jquery

Operations and precautions such as obtaining checkbox selected items in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 17:12:381321browse

1. Get the selected items of the checkbox
2. Select all and unselect the checkbox options

Checkbox code snippet for testing:

Copy code The code is as follows:


                                                id="in1" checked="checked" />
    
    
     
    Fifth grade
                                                                                                                                        label for="in6">Sixth grade
                                                                                                                                       "in7">Seventh grade
                                                                                                                           >Eighth grade
                                                 



1: First of all, let’s talk about the first point, getting the selected item of the checkbox. Most of the methods found on the Internet use each to obtain:

Copy code The code is as follows:

$("input[name='checkbox'][ checked]").each(function () {
alert(this.value);
})
But I encountered a problem when testing. This method works in IE It can be obtained under Firefox and Chrome browsers, but the current selected item cannot be obtained under Firefox and Chrome browsers. The test results are as follows:

Test results under IE (mine is IE10):

The effect under IE10:

The effect under Chrome browser:

I found the reason by searching on google:

Website: Jquery question about how many input CheckBoxes are selected, IE is normal, FF and Chrome cannot get the value

Because the jquery version I am using is 1.7.2, I have to use :checked to obtain it. The modified code:

Copy code The code is as follows:

//Get the selected item
huoqu2').click(function () {
                                                                                                                                    ) {
                                                                                   });

The effect under chrome:

Two: Select all checkboxes and reverse selection operation:

Since these two are relatively simple, I will go directly to the code:

Copy code The code is as follows:

//Select all/Deselect all
                        ('#quanxuan').toggle(function () {
                                                                                                                                         
               $("input[name='abc']").removeAttr("checked");
         });

                                                                                                                                                                                                                                                              . 🎜>                                                                                                                                      $(this).attr("checked ", 'true');
                                                                                                                
To summarize again:

When the jquery version is before 1.3, the operation to obtain the selected item of the checkbox:





Copy code

The code is as follows:

     $("input[name='abc'][ checked]").each(function () {

                                                                                                                                                                                                             ;

When the jquery version is after 1.3, the operation to obtain the selected item of the checkbox:

Copy code
The code is as follows:


checked").each(function () {
                                                                                                                                                                                                                                                                                         ;
Attached is the complete test Demo code:

Copy code The code is as follows:



   
   
   

    <script><br>        $(function () {<br>            //获取选中项(FF和chrome下无效)<br>            $('#huoqu').click(function () {</p> <p>                //$("input[name='abc'][checked]").each(function () {<br>                //    alert(this.value);<br>                //});</p> <p>                $('#show').html("");<br>                $("input[name='abc'][checked]").each(function () {<br>                    //alert(this.value);<br>                    $('#show').append(this.value + "  ");<br>                });<br>            });</p> <p><br>            //获取选中项<br>            $('#huoqu2').click(function () {<br>                $('#show').html("");<br>                $("input[name='abc']:checked").each(function () {<br>                    //alert(this.value);<br>                    $('#show').append(this.value + "  ");<br>                });<br>            });</p> <p><br>            //全选/取消全选<br>            $('#quanxuan').toggle(function () {<br>                $("input[name='abc']").attr("checked", 'true');<br>            }, function () {<br>                $("input[name='abc']").removeAttr("checked");<br>            });</p> <p><br>            //反选<br>            $('#fanxuan').click(function () {<br>                $("input[name='abc']").each(function () {<br>                    if ($(this).attr("checked")) {<br>                        $(this).removeAttr("checked");<br>                    } else {<br>                        $(this).attr("checked", 'true');<br>                    }<br>                });<br>            });<br>        });</p> <p>  </script>




             
                                    

   
    
    
                                                                                                          " value="seventh grade" id="in7" />
    
                                                             ;input type="button" id="huoqu" value="Get selected items (invalid in FF and chrome)" />
                                                                                                                  /Cancel all selections" />
                                                                                                                                                                                                                                                                                        ​"Get the selected item" />
                                                                                                                                                                                    
                                                                                                         Child

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn