jquery 实现只能选中一个checkbox,选中当前的去除上一个

jq 实现只能选中一个checkbox,选中当前的去除上一个。

<div id="checkboxed">  
     <input name="check1" type="checkbox"/>check1  
     <input name="check2" type="checkbox"/>check2  
     <input name="check3" type="checkbox"/>check3  
</div> 

<script type="text/javascript">   
$(function() {
  $('#checkboxed').find('input[type=checkbox]').bind('click', function(){  
          var id = $(this).attr("id");
        
        //当前的checkbox是否选中
        if(this.checked){
            //除当前的checkbox其他的都不选中
            $("#checkboxed").find('input[type=checkbox]').not(this).attr("checked", false); 
            
            //选中的checkbox数量
            var selectleng = $("input[type='checkbox']:checked").length;
            console.log("选中的checkbox数量"+selectleng);
        }else{
            //未选中的处理
            console.log("未选中的处理");
        }
    }); 
})
</script>
            

 

posted @ 2019-09-03 11:44  Ananiah  阅读(2174)  评论(0编辑  收藏  举报