单选按钮和复选框在点击后不显示警告消息,你能看看我是否犯了错误吗?

The radio buttons and checkbox do not show the alert message after clicking, Can you look if I made a mistake?

<!doctype html>

<html>

<head>
<title> Functions </title>
<script>

此功能用于在单个警报消息中显示 2 或 3 个污染物 ppm 水平(在 html 页面上弹出)

function pollutant_level_multiple()
{
if(!document.getElementById("co").checked && 
document.getElementById("no2").checked && 
document.getElementById("voc").checked == true)
{alert("CO=394 ppm");
}
else if(document.getElementById("co").checked && 
!document.getElementById("no2").checked && 
document.getElementById("voc").checked == true)
{alert("CO=394 ppm");
}
else if(document.getElementById("co").checked && 
document.getElementById("no2").checked && 
!document.getElementById("voc").checked == true)
{alert("CO=394 ppm");
}
else if(document.getElementById("co").checked && 
document.getElementById("no2").checked && 
document.getElementById("voc").checked == true)
{alert("CO=394 ppm");
}
}

此功能一次仅显示一种污染物 ppm 级别。

function pollutant_level_individual()
{
if(document.getElementById("co").checked == true)
{alert("CO=394 ppm");
}
else if(document.getElementById("no2").checked == true)
{alert("CO=394 ppm");
}
else if(document.getElementById("voc").checked == true)
{alert("CO=394 ppm");
}
}
</script>
</head>

<body>
<form>

Which of the following pollutant level would you like to know?
<ol>

我使用单选按钮让用户可以自由地单独或同时查看多个污染物水平。 'onclick' 是在 <script> 标签中执行相应功能的那个。

Would you like to view ppm levels individually or multiple values at once? 
&nbsp Individually: <input id = "radio1" type = "radio" name = "indimul" 
onclick="pollutant_level_individual();"/> &nbsp Multiple at once: <input id 
= "radio2" type = "radio" name = "indimul" 
onclick="pollutant_level_multiple();" /> 
<li> Carbon-monoxide: <input id = "co" type = "checkbox" name = "CO 
indicator" onclick = "pollutant_level_multiple();"/>  </li>
<li> Nitrogen-dioxide: <input id = "no2" type = "checkbox" name = "NO2 
indicator" onclick="pollutant_level_multiple();"/> </li>
<li> Volatile Organic Compounds: <input id = "voc" type = "checkbox" name = 
"VOC indicator" onclick="pollutant_level_multiple();"/> </li>
</ol>

</form>

</html>

其实我收到你的问题了,上面的代码有很多地方需要改进。

  1. If 条件 - 在多种情况下使用 else if 应该是不同的
  2. Individual/Multiple是代码的方向,缺少驱动函数我加了

请在下面找到工作片段,如果需要进一步的帮助,请告诉我。

        var ClickFun;

       function setTheAlertFlow(flow){
          if(flow=="mutliple"){
            ClickFun=pollutant_level_multiple;
          }else{
              ClickFun =pollutant_level_individual;
          };
          ClickFun();
       }
        function pollutant_level_multiple() {
            var ppmLevels = '';

            if(document.getElementById("co").checked ){
                ppmLevels += "CO=394 ppm" + " "
            };

            if(document.getElementById("no2").checked ){
                ppmLevels += "NO2=394 ppm" + " "
            }

            if(document.getElementById("voc").checked ){
                ppmLevels += "VOC=394 ppm"
            }
             

             if(ppmLevels){
                alert(ppmLevels)
             }else{
                 alert("PPM No PPM checkboxes selected")
             }
            
                
                
        }
        function pollutant_level_individual() {
            if (document.getElementById("co").checked == true) {
                alert("CO=394 ppm");
            }
             if (document.getElementById("no2").checked == true) {
                alert("NO=394 ppm");
            }
             if (document.getElementById("voc").checked == true) {
                alert("VOC=394 ppm");
            }
        }
<!doctype html>

<html>

<head>
    <title> Functions </title>
    <script>

    </script>
</head>

<body>
    <form>

        Which of the following pollutant level would you like to know?
        <ol>
            Would you like to view ppm levels individually or multiple values at once? &nbsp Individually:
            <input id="radio1"radioG type="radio" name="indimul" value="individual" onclick="setTheAlertFlow(this.value);" /> &nbsp Multiple at once:
            <input id="radio2" type="radio" name="indimul" value="mutliple" onclick="setTheAlertFlow(this.value);" />
            <li> Carbon-monoxide:
                <input id="co" type="checkbox" name="CO indicator" onclick="ClickFun();" /> </li>
            <li> Nitrogen-dioxide:
                <input id="no2" type="checkbox" name="NO2 indicator" onclick="ClickFun();" /> </li>
            <li> Volatile Organic Compounds:
                <input id="voc" type="checkbox" name="VOC indicator" onclick="ClickFun();" /> </li>
        </ol>

    </form>

</html>

你的问题有点不清楚,但我认为你正在寻找这样的东西:

var polutantsPPM = { CO: 394, NO2: 224, VOC: 122 };

function getReportString(polutantId, polutantsPPM){ return polutantId + " = " +  polutantsPPM[polutantId] + "ppm"; }

function pollutant_level_individual() {
    if(document.getElementById("co").checked) { alert(getReportString("CO", polutantsPPM)); }
    if(document.getElementById("no2").checked) { alert(getReportString("NO2", polutantsPPM)); }
    if(document.getElementById("voc").checked) { alert(getReportString("VOC", polutantsPPM)); }
}

function pollutant_level_multiple() {
    var msg = [];
    if(document.getElementById("co").checked) { msg.push(getReportString("CO", polutantsPPM)); }
    if(document.getElementById("no2").checked) { msg.push(getReportString("NO2", polutantsPPM)); }
    if(document.getElementById("voc").checked) { msg.push(getReportString("VOC", polutantsPPM)); }
    alert(msg.join("\n"));
}
<p>
    Which of the following pollutant level would you like to know?
</p>

<ol>
    <li>Carbon-monoxide: <input id="co" type="checkbox" name="CO indicator"/></li>
    <li>Nitrogen-dioxide: <input id="no2" type="checkbox" name="NO2 indicator"/></li>
    <li>Volatile Organic Compounds: <input id="voc" type="checkbox" name="VOC indicator"/></li>
</ol>

<p>
    Would you like to view ppm levels individually or multiple values at once? &nbsp;
    Individually: <input id="radio1" type="radio" name="indimul" onclick="pollutant_level_individual();"/> &nbsp;
    Multiple at once: <input id="radio2" type="radio" name="indimul" onclick="pollutant_level_multiple();" />
</p>