chckbox 实现单选效果(html)

来源:互联网 发布:无人深空优化差 编辑:程序博客网 时间:2024/05/20 02:22
       note:在html <input> 标签类中的checkbox实现单选效果。
       在最近的开发项目中,客户要求使用小方格子实现“单选”功能,显然圆点的radio被out了,只能选择chckbox的方块样式,也在网上搜过,可能有点儿脑残,没有找到。
       废话不多说直接上代码:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  5.     <title>checkbox实现单选效果</title>  
  6.     <script src="jquery-1.7.2.min.js"></script>  
  7.     <script>  
  8.         $(document).ready(function () {  
  9.             $('.sb').click(function () {  
  10.                 var typeThis = typeof ($(this).attr("checked"));  
  11.                 switch (typeThis) {  
  12.                     case "string":  
  13.                         if ($(this).attr("checked").toLowerCase()=="checked") {  
  14.                             $('.sb').each(function () {  
  15.                                 $(this).removeAttr("checked");  
  16.                             });  
  17.                             $(this).attr("checked",true);  
  18.                         }  
  19.                         break;  
  20.                     case "undefined":  
  21.                         $('.sb').each(function () {  
  22.                             $(this).removeAttr("checked");  
  23.                         });  
  24.                         break;  
  25.                 }  
  26.             });  
  27.         });  
  28.     </script>  
  29. </head>  
  30. <body>  
  31.     <input class="sb" type="checkbox" value="1"/>  
  32.     <input class="sb" type="checkbox" value="2" />  
  33.     <input class="sb" type="checkbox" value="3" />  
  34.     <input class="sb" type="checkbox" value="4" />  
  35. </body>  
  36. </html>
0 0
原创粉丝点击