首页 > Web开发 > 详细

jQuery 判断checkbox是否被选中 4种方法

时间:2015-12-31 01:34:08      阅读:265      评论:0      收藏:0      [点我收藏+]
下午写JS验证,有一个需求需要判断 checkbox是否被选择,查阅相关资料后,总结以下4种方法,分享给大家。

<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery 判断checkbox是否被选中 4种方法</title> <script src="jquery-1.8.3.min.js"></script> <script> $(function(){ $(#agree).on(click, function(){ if($("input[type=‘checkbox‘]").is(:checked)){ // 选中返回true,未选中返回false console.log(已选中); }else{ console.log(未选中); } }) $(#agree2).on(change, function(){ // 可以是 onchange ,也可以是 onclick if($(#agree2).attr(checked)){ console.log(已选中); }else{ console.log(未选中); } }) $(#agree3).on(change, function(){ if($("#agree3").get(0).checked){ console.log(已选中); }else{ console.log(未选中); } }) $(#agree4).on(change, function(){ if($("#agree4").prop(checked)){ console.log(已选中); }else{ console.log(未选中); } }) }) </script> </head> <body> <input type="checkbox" name="agree" id="agree"> 同意此协议 <br> <br> <input type="checkbox" name="agree" id="agree2"> 同意此协议2 <br> <br> <input type="checkbox" name="agree" id="agree3"> 同意此协议3 <br> <br> <input type="checkbox" name="agree" id="agree4"> 同意此协议4 <br> <br> </body> </html>

 

jQuery 判断checkbox是否被选中 4种方法

原文:http://www.cnblogs.com/slion/p/5090410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!