Get checkbox status using javascript

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

This is my checkbox HTML code

 class=\"che         


        
5条回答
  •  佛祖请我去吃肉
    2021-01-02 06:02

    Live Demo (Click the "Terms Div" text to test)

    I didnt see the question tagged with jQuery, but I noticed a jQery selector was used.. so just to be safe I did it with pure JS anyway.

    Pure JS

    var terms = document.getElementById("termsCheckbox"),
        terms_div = document.getElementById("terms_div");
    
    function validateTerms(){
        if(terms.checked == false){
            if(terms_div.className.indexOf("terms_error")<0){
                terms_div.className += " terms_error";
            }
            return false;
        }else{      
            terms_div.className = terms_div.className.replace(/\bterms_error\b/,'');
            return true;
        }
    }
    

提交回复
热议问题