js实现功能比较全面的全选和多选

js实现功能比较全面的全选和多选

代码如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> </head> <body> <input type="checkbox" value="1" />水果 <br /> <input type="checkbox" value="${article.id}" />奶茶<br /> <input type="checkbox" value="${article.id}"/>香蕉<br /> <input type="checkbox" value="${article.id}"/>橘子<br /> <input type="checkbox" value="${article.id}"/>苹果<br /> <input type="checkbox" value="${article.id}"/>梨子<br /> <script type="text/javascript"> function quanxuan(){ var $checkbox = $(".item"); var $checked = $checkbox.filter(":checked"); if($checkbox.length == $checked.length){ $("#checkbox").prop("checked",true); }else{ $("#checkbox").prop("checked",false); } } $(function(){ $("#checkbox").on("change",function(){ var checked = $(this).prop("checked"); // true / false $(".item").prop("checked",checked); quanxuan(); }); $("body").on("change" , ".item",function(e){ quanxuan(); }); }); </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwzzyz.html