<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input type="checkbox" class="hobby" name="hobby" value="swim" />游泳 <input type="checkbox" class="hobby" name="hobby" value="tv" checked="checked" />看电视 <input type="checkbox" class="hobby" name="hobby" value="run" />跑步 <input type="checkbox" class="hobby" name="hobby" value="sport" />运动 <input type="button" id="b" value="取值" /> <script type="text/javascript" src="../images/jquery-3.4.1.min.js"></script> <script type="text/javascript"> $("#b").click(function(){ $(".hobby").each(function(index,item){ /* item为DOM对象 */ /* var val = $(item).attr("value"); */ /* attr能取到就是checked的值,不能取到就是undefined */ /* var val = $(item).attr("checked"); */ /* prop函数能取到checke的值就是true,不能取到就是false 只针对checked,visibled,selected,disabled*/ /* var val = $(item).prop("checked"); alert("值为:"+ val); */ /* 为所有匹配元素设置一个属性值 */ $("input").prop("class","hobb"); }) }); </script> </body> </html>