检查是否选中了复选框

时间:2013-06-25 21:39:19

标签: javascript html forms

这是我网站上表单的底部部分。在添加TOS协议复选框之前,一切正常。现在,无论是否选中该框,它都会提醒用户必须选中该框。任何帮助将不胜感激。

<input type='checkbox' value='yes' name='tosagree' id='tosagree'> <p>I have read and agree to the privacy policy and terms of use found on this site.</p>
            <input type='submit' id='submit' name='submit' value='submit'><br /><br />
    </form>
</div>

<script type='text/javascript'>
function validateForm()
{
var asking=document.forms["form"]["asaskingprice"].value,
reasons=document.forms["form"]["asreasons"].value,
timelines=document.forms["form"]["astimelines"].value,
sources=document.forms["form"]["assources"].value,
tos=document.forms["form"]["tosagree"].value;
if (asking==null || asking=="")
  {
  alert("Asking price must be filled out");
  return false;
  }
if (reasons==null || reasons=="")
  {
  alert("reasons must be filled out");
  return false;
  }
if (timelines==null || timelines=="")
  {
  alert("timelines must be filled out");
  return false;
  }
if (sources==null || sources=="")
  {
  alert("sources must be filled out");
  return false;
  }
if (document.forms["form"]["tosagree"].Checked!=true)
  {
  alert("You must agree to the terms of service");
  return false;
  }
}
</script>

2 个答案:

答案 0 :(得分:5)

正确的属性名称是checked,而不是Checked

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-checked

答案 1 :(得分:0)

你可以通过它的id抓住那个吸盘:

var tos = document.getElementById('tosagree');

if (tos.checked) {
    //return true
}