enable or disable checkbox in html

前端 未结 6 1329
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 10:26

I want to enable or disable checkbox in table\'s row on basis of condition.

code -



        
6条回答
  •  执念已碎
    2021-01-01 11:13

    I know this question is a little old but here is my solution.

     // HTML
     // 
     // 
    
     // cb1 = checkbox 1
     // cb2 = checkbox 2
      var cb1 = getId('cb1'), 
          cb2 = getId('cb2');
    
      function getId(id) {
        return document.getElementById(id);
      }
    
      function setcb1() {
        if(cb1.checked === true) {
          cb2.checked = false;
          cb2.disabled = "disabled"; // You have to disable the unselected checkbox
        } else if(cb1.checked === false) {
          cb2.disabled = ""; // Then enable it if there isn't one selected.  
        }
      }
    
      function setcb2() {
        if(cb2.checked === true) {
          cb1.checked = false;
          cb1.disabled = "disabled"
        } else if(cb2.checked === false) {
          cb1.disabled = ""
      }
    

    Hope this helps!

提交回复
热议问题