Home >Web Front-end >JS Tutorial >Operations and precautions such as obtaining checkbox selected items in jQuery_jquery
1. Get the selected items of the checkbox
2. Select all and unselect the checkbox options
Checkbox code snippet for testing:
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:
The effect under IE10:
The effect under Chrome browser:
I found the reason by searching on google:
Because the jquery version I am using is 1.7.2, I have to use :checked to obtain it. The modified code:
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:
. 🎜> $(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
$("input[name='abc'][ checked]").each(function () {
;When the jquery version is after 1.3, the operation to obtain the selected item of the checkbox:
<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>