Javascript 使用innerHTML根据复选框更改div值

Javascript 使用innerHTML根据复选框更改div值,javascript,forms,innerhtml,Javascript,Forms,Innerhtml,我试图根据是否选中复选框来更改div中的文本。我是Javascript新手,所以这就是我所尝试的。。。如果有人能告诉我哪里出轨了,我将不胜感激 函数SetContent(){ if(document.complianceformAA.compliance\u AA.checked==true){ document.getElementById('ComplianceSummary').innerHTML=“发现关键元素I-A存在合规问题”; }否则{ document.getElementBy

我试图根据是否选中复选框来更改div中的文本。我是Javascript新手,所以这就是我所尝试的。。。如果有人能告诉我哪里出轨了,我将不胜感激

函数SetContent(){
if(document.complianceformAA.compliance\u AA.checked==true){
document.getElementById('ComplianceSummary').innerHTML=“发现关键元素I-A存在合规问题”;
}否则{
document.getElementById('ComplianceSummary')。innerHTML=“满足关键元素I-A。”;
}
}

合规问题?
生成摘要
您不应该使用,而是通过
id
使用

函数SetContent(){
if(document.getElementById('compliance_AA')。选中){
document.getElementById('ComplianceSummary').innerHTML=“发现关键元素I-A存在合规问题”;
}否则{
document.getElementById('ComplianceSummary')。innerHTML=“满足关键元素I-A。”;
}
}

合规问题?
生成摘要

首先,缩进代码:

function setContent() {
  if (document.complianceformAA.compliance_AA.checked == true) {
    document.getElementById('ComplianceSummary').innerHTML="A compliance concern is found for Key Element I-A.";
  } else {
   document.getElementById('ComplianceSummary').innerHTML="Key Element I-A is met.";
  }
}
其次,你不会得到这样的元素。 您可以通过以下方法之一获得:

  • 这将基于ID获取元素。例如:

    document.getElementById('checkbox');
    
    你会得到这个:

    <input type="checkbox" id="checkbox"></input>
    
    可能会回来

    HTMLCollection [ <button>, <button>, <button>, <button> ]
    
    HTMLCollection [ <button.a>, <button.a>, <button.a>, <button.a> ]
    
    NodeList [ <button.className> <button.className> ]
    
    可能会回来

    HTMLCollection [ <button>, <button>, <button>, <button> ]
    
    HTMLCollection [ <button.a>, <button.a>, <button.a>, <button.a> ]
    
    NodeList [ <button.className> <button.className> ]
    
    可能会回来

    HTMLCollection [ <button>, <button>, <button>, <button> ]
    
    HTMLCollection [ <button.a>, <button.a>, <button.a>, <button.a> ]
    
    NodeList [ <button.className> <button.className> ]
    
    下面是一个例子:

    函数setContent(){
    复选框=document.getElementById('compliance_AA');
    如果(复选框。选中){
    document.getElementById('ComplianceSummary').innerHTML=“发现关键元素I-A存在合规问题”;
    }否则{
    document.getElementById('ComplianceSummary')。innerHTML=“满足关键元素I-A。”;
    }
    }
    
    合规问题?
    生成摘要
    
    function setContent() {
      checkBox = document.getElementById('compliance_AA');
      if (checkBox.checked) {
        document.getElementById('ComplianceSummary').innerHTML = "A compliance concern is found for Key Element I-A.";
      } else {
        document.getElementById('ComplianceSummary').innerHTML = "Key Element I-A is met.";
      }
    }
    
    function SetContent(){
       var chk = document.getElementById("compliance_AA");
       var summary = document.getElementById("ComplianceSummary");
    
       if(chk.checked){
        summary.innerHTML="A compliance concern is found for Key Element I-A."
       }else{
        summary.innerHTML="Key Element I-A is met."
       }
    }