复选框值无效

时间:2016-08-25 13:14:26

标签: javascript

我想要显示"你好"或者" Shalom"当选中相应的复选框但我得到未定义时:

<!DOCTYPE html>
<html>
<head>
<script>
    function clicking(cb) {
               alert(cb.checked.value);
               //alert(cb.checked) this works fine, it displays true or false
}
</script>
<meta charset=utf-8 />
<title>DOM0 Checkbox Click Event Example</title>

</head>
<body>

  <label for="cb1"><input id="cb1" type="checkbox" value = "Shalom" onclick="clicking(this);">Hello</label>
 <label for="cb2"><input id="cb2" type="checkbox" value = "shalom" onclick="clicking(this);">Shalom</label>
</body>
</html>

请帮助我,但不使用具有此模板的JS函数: getElementBys omething()

2 个答案:

答案 0 :(得分:0)

You can use 

   <script>
function check() {
    document.getElementById("myCheck").checked = true;
}

function uncheck() {
    document.getElementById("myCheck").checked = false;
}
</script>

Checkbox: <input type="checkbox" id="myCheck">

<button onclick="check()">Check Checkbox</button>
<button onclick="uncheck()">Uncheck Checkbox</button>

答案 1 :(得分:0)

使用此:

 function clicking(cb)
 {
     if(document.getElementById("cb1").checked = true)
     {
        alert(document.getElementById("cb1").value);
      }
     if(document.getElementById("cb1").checked = true)
     {
        alert(document.getElementById("cb2").value);
     }
 }