使用javascript获取复选框状态 [英] Get checkbox status using javascript

查看:102
本文介绍了使用javascript获取复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的复选框HTML代码

This is my checkbox HTML code

<input id="termsCheckbox" name="termsCheckbox" type="checkbox" value="terms" <?PHP echo $terms; ?> class="checkbox">

这是JavaScript代码

this is javascript code

var terms = $("#termsCheckbox");

function validateTerms(){
if(termsCheckbox.checked == false){
terms_div.addClass("terms_error");
return false;
}
else{           
terms_div.removeClass("terms_error");
return true;
}
}

我要检查复选框是否已选中以及是否不要将一个类添加到terms_div。请帮我解决这个问题。谢谢

I want to check whether checkbox checked or not and if not add a class to terms_div. Please help me to solve this problem. thanks

推荐答案

您需要访问 className 变量(纯JS)以下假设您的 div 的I​​D为 terms_div ,即 terms_error 是您可能要在 div 上使用的唯一类,并且您可以通过 onClick = validateTerms();

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;
  }
}

这篇关于使用javascript获取复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆