JQuery 多個checkbox 只選中一個

    <form id="common-form">  
        <input name="check1" type="checkbox"/>check1  
        <input name="check2" type="checkbox"/>check2  
        <input name="check3" type="checkbox"/>check3  
    </form>  
$(function() {
  $('#common-form').find('input[type=checkbox]').bind('click', function(){  
          var id = $(this).attr("id");
        
        //當前的checkbox是否選中
        if(this.checked){
            //除當前的checkbox其他的都不選中
            $("#common-form").find('input[type=checkbox]').not(this).attr("checked", false); 
            
            //選中的checkbox數量
            var selectleng = $("input[type='checkbox']:checked").length;
            console.log("選中的checkbox數量"+selectleng);
        }else{
            //未選中的處理
            console.log("未選中的處理");
        }
    }); 
})


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章