Get checkbox status using javascript

前端 未结 5 1465
时光说笑
时光说笑 2021-01-02 05:40

This is my checkbox HTML code

 class=\"che         


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 06:20

    You need to access the className variable (pure JS) the following assumes your div has an ID of terms_div, that terms_error is the only class you might want on the div, and that you setup your checkbox with onClick="validateTerms();"

    function validateTerms(){
      var c=document.getElementById('termsCheckbox');
      var d=document.getElementById('terms_div');
      if (c.checked) {
        d.className='';
        return true;
      } else { 
        d.className='terms_error';
        return false;
      }
    }
    

提交回复
热议问题