Home >Web Front-end >JS Tutorial >jquery handles whether the checkbox check box is selected example code sharing
This article mainly introduces the relevant information of jquery processing checkbox (checkbox) whether it is selected example code. Friends who need it can refer to it. I hope it can help everyone.
jquery handles whether the checkbox (checkbox) is selected
Now if a checkbox is selected, use checked=true, checked="checked" also Line
It would be better to use prop instead of attr. Although the attr() method can be used normally in versions before jQuery1.6, the prop() method must be used instead
Example code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>checkbox</title> </head> <body> <input type="button" id="btn1" value="全选"> <input type="button" id="btn2" value="取消全选"> <input type="button" id="btn3" value="选中所有奇数"> <input type="button" id="btn4" value="反选"> <input type="button" id="btn5" value="获得选中的所有值"> <input type="checkbox" value="checkbox1"/> <input type="checkbox" value="checkbox2"/> <input type="checkbox" value="checkbox3"/> <input type="checkbox" value="checkbox4"/> <input type="checkbox" value="checkbox5"/> <script src="js/jquery-3.2.0.min.js"></script> <script> $(function(){ var checkbox = $("input[type=checkbox]"); $("#btn1").on("click",function(){ checkbox.prop("checked",true); }); $("#btn2").on("click",function(){ checkbox.prop("checked",false); }); $("#btn3").on("click",function(){ $("input[type=checkbox]:even").prop("checked",true); }); $("#btn4").on("click",function(){ checkbox.each(function(){ if($(this).prop("checked")){ $(this).prop("checked",false); }else{ $(this).prop("checked",true); } }); }); $("#btn5").on("click",function(){ var str = ""; $("input[type=checkbox]").each(function(){ if($(this).prop("checked")){ str += $(this).val() + ","; } }); console.log(str); }); }); </script> </body> </html>
Related recommendations:
Determining whether the radio in the input box in html is selected
Summary jquery to determine whether a check box is selected example
jquery code to determine whether a single check box is selected_jquery
The above is the detailed content of jquery handles whether the checkbox check box is selected example code sharing. For more information, please follow other related articles on the PHP Chinese website!