猿问

我如何使这段代码给出预期的结果

function myfun(){

   //var yofad = document.getElementById('age').value;

   var yoad;

   var year = 2014 ;

     if( year >= 2014){

        yoad = "Your certicate is ready go to school portal";

      } else if (year == 2019){

        yoad = "Your certificate is in progress";

      } else {

        yoad = "cert not found";

      }

document.getElementById('cc').innerHTML=yoad

}

<h6>Enter your year of admission to know if your certificate is ready</h6>

<input id="age">

<button onclick="myfun()">enter</button>

<p id="xc"></p>

<p id="cc"></p>

当输入介于 2014 和 2019 之间时,我希望输出为“您的证书已准备好去学校门户网站”,当输入高于 2019 时,输出应为 cert is not read



HUWWW
浏览 179回答 3
3回答

江户川乱折腾

只需使用并操作它会帮助您检查 2 个条件function myfun(){&nbsp; &nbsp;//var yofad = document.getElementById('age').value;&nbsp; &nbsp;var yoad;&nbsp; &nbsp;var year = 2014 ;&nbsp; &nbsp; &nbsp;if( year >= 2014 && year <2019){&nbsp; &nbsp; &nbsp; &nbsp; yoad = "Your certicate is ready go to school portal";&nbsp; &nbsp; &nbsp; }else if (year == 2019){&nbsp; &nbsp; &nbsp; &nbsp;yoad = "Your certificate is in progress";&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; yoad = "cert not found";&nbsp; &nbsp; &nbsp; }document.getElementById('cc').innerHTML=yoad}

慕丝7291255

将您的第一个条件限制为小于 2019,否则对于 2019、2020 和其他更大的值也将成立。改为if(year >= 14)_if(year >= 14 && year <2019)function myfun(){&nbsp; &nbsp;var year = document.getElementById('age').value;&nbsp; &nbsp;var yoad;&nbsp; &nbsp; &nbsp;if( year >= 2014 && year < 2019){&nbsp; &nbsp; &nbsp; &nbsp; yoad = "Your certicate is ready go to school portal";&nbsp; &nbsp; &nbsp; } else if (year == 2019){&nbsp; &nbsp; &nbsp; &nbsp; yoad = "Your certificate is in progress";&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; yoad = "cert not found";&nbsp; &nbsp; &nbsp; }document.getElementById('cc').innerHTML=yoad}<h6>Enter your year of admission to know if your certificate is ready</h6><input id="age"><button onclick="myfun()">enter</button><p id="xc"></p><p id="cc"></p>

桃花长相依

function myfun(){&nbsp; document.getElementById('cc').innerHTML = "";&nbsp; const value = document.getElementById('age').value;&nbsp; if (!value) return;&nbsp;&nbsp;&nbsp; const year = parseInt(value);&nbsp; let yoad;&nbsp;&nbsp;&nbsp; if (year > 2019) {&nbsp; &nbsp; yoad = "certicate not read";&nbsp; } else if (year >= 2014 && year <= 2019) {&nbsp; &nbsp; yoad = "Your certicate is ready go to school portal";&nbsp; } else {&nbsp; &nbsp; yoad = "cert not found";&nbsp; }&nbsp;&nbsp;&nbsp; document.getElementById('cc').innerHTML = yoad}<h6>Enter your year of admission to know if your certificate is ready</h6><input id="age"><button onclick="myfun()">enter</button><p id="xc"></p><p id="cc"></p>也许你需要这样的东西!
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答