警报弹出消息不显示

时间:2020-01-22 11:14:28

标签: javascript

因此,根据此调查表中的输入,应通过单击提交按钮显示包含分数变量的弹出消息。由于某种原因没有发生。问题到底在哪里?在Visual Studio代码中,没有出现错误,但是当我在浏览器中完成问卷并按提交时,什么也没发生。

HTML

<!doctype html>

<html>

<head>

</head>

<body>

  <h1>Endometrial Cancer Predictor </h1>

  <h3>What is your BMI?</h3>

  <input id="bmi1" type="radio" name="grp1" value="0"> less than 25 </input> <br>

  <input id="bmi2" type="radio" name="grp1" value="1.85"> 25 to 29 </input> <br>

  <input id="bmi3" type="radio" name="grp1" value="4.675"> 30 to 39 </input> <br>

  <input id="bmi4" type="radio" name="grp1" value="6.175"> over 40 </input>


  <h3>Do you take contraception?</h3>

  <input id="contraNo" type="radio" name="grp2" value="0"> No <br>

  <input id="contraOral" type="radio" name="grp2" value="-0.8"> Oral Contraception <br>

  <input id="contraIud" type="radio" name="grp2" value="-1.2"> Intrauterine Device (IUD)


  <h3>Do you take HRT?</h3>

  <input id="hrtNo" type="radio" name="grp3" value="0"> No <br>

  <input id="hrtConti" type="radio" name="grp3" value="-0.675"> Continuous <br>

  <input id="hrtNon" type="radio" name="grp3" value="0.5"> Non Continuous


  <h3>Do you suffer from Type 2 Diabetes?</h3>

  <input id="diabYes" type="radio" name="grp4" value="1.375"> Yes <br>

  <input id="diabNo" type="radio" name="grp4" value="0"> No


  <h3>Do you suffer from PCOS?</h3>

  <input id="pcosYes" type="radio" name="grp5" value="3.75"> Yes <br>

  <input id="pcosNo" type="radio" name="grp5" value="0"> No


  <h3>What is your parity?</h3>

  <input id="nulliparity" type="radio" name="grp6" value="1.075"> 0 <br>

  <input id="parOver1" type="radio" name="grp6" value="-0.8"> +1

  <br>
  <br>

  <button id="btn1" onclick "fn1()">Submit </button>


  <script>
    src = "index.js"
  </script>

</body>

</html>

index.js

function fn1()
{
    var score=2.5;
    var bmi1 = document.getElementById("bmi1");
    var bmi2 = document.getElementById("bmi2");
    var bmi3 = document.getElementById("bmi3");

    var contraNo = document.getElementById("contraNo");
    var contraOral = document.getElementById("contraOral");
    var contraIud = document.getElementById("contraIud");

    var hrtNo = document.getElementById("hrtNo");
    var hrtConti = document.getElementById("hrtConti");
    var hrtNon = document.getElementById("hrtNon");

    var diabYes = document.getElementById("diabYes");
    var diabNo = document.getElementById("diabNo");

    var pcosYes = document.getElementById("pcosYes");
    var pcosNo = document.getElementById("pcosNo");

    var nulliparity = document.getElementById("nulliparity");
    var parOver1 = document.getElementById("parOver1");


    if(bmi1.checked==true) 
        score=score+bmi1.value;
    else if(bmi2.checked==true)
        score=score+bmi2.value;
    else if(bmi3.checked==true)
        score=score+bmi3.value;
    else if(bmi4.checked==true)
        score=score+bmi4.value;

    if(contraNo.checked==true) 
        score=score+contraNo.value;
    else if(contraOral.checked==true)
        score=score+contraOral.value;
    else if(contraIud.checked==true)
        score=score+contraIud.value;

    if(hrtNo.checked==true) 
        score=score+hrtNo.value;
    else if(hrtConti.checked==true)
        score=score+hrtConti.value;
    else if(hrtNon.checked==true)
        score=score+hrtNon.value;

    if(diabYes.checked==true) 
        score=score+diabYes.value;
    else if(diabNo.checked==true)
        score=score+diabNo.value;

    if(pcosYes.checked==true) 
        score=score+pcosYes.value;
    else if(pcosNo.checked==true)
        score=score+pcosNo.value;

    if(nulliparity.checked==true) 
        score=score+nulliparity.value;
    else if(parOver1.checked==true)
        score=score+parOver1.value;


       return score;

}
function displayResult() {
    var result = fn1();
    document.getElementById('btn1').innerHTML = result;
    return false;
}

1 个答案:

答案 0 :(得分:0)

根据您的代码段,我假设您需要在onclick上运行displayResult()函数。 fn1()没有执行任何警报或DOM操作,只是返回了score的值。在下面查看此答案附带的稍作更新的演示。

另外,如果您希望在弹出窗口中显示结果,则需要在alert()内使用displayResult()函数

function fn1() {

  var score = 2.5;
  var bmi1 = document.getElementById("bmi1");
  var bmi2 = document.getElementById("bmi2");
  var bmi3 = document.getElementById("bmi3");

  var contraNo = document.getElementById("contraNo");
  var contraOral = document.getElementById("contraOral");
  var contraIud = document.getElementById("contraIud");

  var hrtNo = document.getElementById("hrtNo");
  var hrtConti = document.getElementById("hrtConti");
  var hrtNon = document.getElementById("hrtNon");

  var diabYes = document.getElementById("diabYes");
  var diabNo = document.getElementById("diabNo");

  var pcosYes = document.getElementById("pcosYes");
  var pcosNo = document.getElementById("pcosNo");

  var nulliparity = document.getElementById("nulliparity");
  var parOver1 = document.getElementById("parOver1");


  if (bmi1.checked == true)
    score = score + bmi1.value;
  else if (bmi2.checked == true)
    score = score + bmi2.value;
  else if (bmi3.checked == true)
    score = score + bmi3.value;
  else if (bmi4.checked == true)
    score = score + bmi4.value;

  if (contraNo.checked == true)
    score = score + contraNo.value;
  else if (contraOral.checked == true)
    score = score + contraOral.value;
  else if (contraIud.checked == true)
    score = score + contraIud.value;

  if (hrtNo.checked == true)
    score = score + hrtNo.value;
  else if (hrtConti.checked == true)
    score = score + hrtConti.value;
  else if (hrtNon.checked == true)
    score = score + hrtNon.value;

  if (diabYes.checked == true)
    score = score + diabYes.value;
  else if (diabNo.checked == true)
    score = score + diabNo.value;

  if (pcosYes.checked == true)
    score = score + pcosYes.value;
  else if (pcosNo.checked == true)
    score = score + pcosNo.value;

  if (nulliparity.checked == true)
    score = score + nulliparity.value;
  else if (parOver1.checked == true)
    score = score + parOver1.value;


  return score;

}

function displayResult() {
  var result = fn1();
  document.getElementById('btn1').innerHTML = result;
  alert(result);
  return false;
}
<h1>Endometrial Cancer Predictor </h1>

<h3>What is your BMI?</h3>

<input id="bmi1" type="radio" name="grp1" value="0"> less than 25 <br>

<input id="bmi2" type="radio" name="grp1" value="1.85"> 25 to 29<br>

<input id="bmi3" type="radio" name="grp1" value="4.675"> 30 to 39 <br>

<input id="bmi4" type="radio" name="grp1" value="6.175"> over 40


<h3>Do you take contraception?</h3>

<input id="contraNo" type="radio" name="grp2" value="0"> No <br>

<input id="contraOral" type="radio" name="grp2" value="-0.8"> Oral Contraception <br>

<input id="contraIud" type="radio" name="grp2" value="-1.2"> Intrauterine Device (IUD)


<h3>Do you take HRT?</h3>

<input id="hrtNo" type="radio" name="grp3" value="0"> No <br>

<input id="hrtConti" type="radio" name="grp3" value="-0.675"> Continuous <br>

<input id="hrtNon" type="radio" name="grp3" value="0.5"> Non Continuous


<h3>Do you suffer from Type 2 Diabetes?</h3>

<input id="diabYes" type="radio" name="grp4" value="1.375"> Yes <br>

<input id="diabNo" type="radio" name="grp4" value="0"> No


<h3>Do you suffer from PCOS?</h3>

<input id="pcosYes" type="radio" name="grp5" value="3.75"> Yes <br>

<input id="pcosNo" type="radio" name="grp5" value="0"> No


<h3>What is your parity?</h3>

<input id="nulliparity" type="radio" name="grp6" value="1.075"> 0 <br>

<input id="parOver1" type="radio" name="grp6" value="-0.8"> +1

<br>
<br>

<button id="btn1" onclick="displayResult()">Submit</button>