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>
            

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM