我知道这应该很简单,但我找不到一种方法去做,我有一种感觉很简单,但我被卡住了。每次选中复选框时,我都会尝试显示一条消息。我有下面的代码,但我只能显示第一个复选框的消息。我刚刚开始使用Javascript。
function myFunction() {
    var checkBox = document.getElementById("myCheck");
    var text = document.getElementById("text");
    if (checkBox.checked == true){
        text.style.display = "block";
    } else {
       text.style.display = "none";
    }
}<!DOCTYPE html>
<html>
<body>
<p>Display some text when the checkbox is checked:</p>
Checkbox: <input type="checkbox" id="myCheck"  onclick="myFunction()">
<p id="text" style="display:none">Checkbox is CHECKED!</p>
Checkbox2: <input type="checkbox" id="myCheck"  onclick="myFunction()">
<p id="text" style="display:none">Second checkbox is checked</p>
Checkbox3: <input type="checkbox" id="myCheck"  onclick="myFunction()">
<p id="text" style="display:none">Third checkbox is checked</p>
</body>
</html>
发布于 2018-03-16 11:32:56
首先,你需要有输入类型的复选框和uniq id。
<input type="checkbox" id="uniq_id" />然后在脚本中调用您的函数
$("#uniq_id").click( function(){
  if( $(this).is(':checked') ){ 
     //call your function here
     myFunction(this);  
   }
});https://stackoverflow.com/questions/49312676
复制相似问题