猿问

如果在 JavaScript 中总是相同的结果

我该如何解决这个问题,我总是得到 <18 结果我尝试了数字 1-100 但结果总是 <18 的文本


In HTML i have <p> with id="rezultat", input with id "godine" and buttno with id "btn"


JavaScript

let = document.getElementById("rezultat");

let dugme = document.getElementById("btn");

let god = document.getElementById("godine");


function myFunction() {


if (god >= 18){

    document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";

}

if (god <= 65){

    document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";

}

if (god < 18){

    document.getElementById("rezultat").innerHTML = "Imate premalo godina";

}

if (god > 65){

    document.getElementById("rezultat").innerHTML = "Imate previse godina";

}

}


万千封印
浏览 135回答 3
3回答

明月笑刀无情

由于函数中的最后两个似乎很重要 - 太年轻或太老 - 从这些开始,如果用户不是其中之一,则他们属于正确的年龄组:function myFunction() {&nbsp; if (god < 18) {&nbsp; &nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Imate premalo godina";&nbsp; } else if (god > 65) {&nbsp; &nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Imate previse godina";&nbsp; } else {&nbsp; &nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";&nbsp; }}所以,如果他们未满18岁,他们就太年轻了。如果他们超过 65 岁,就太老了。否则,他们可以成功注册。另外,请确保您从“godine”元素中获取一个数字 - 例如:let god = parseInt(document.getElementById("godine").value);

慕村225694

let = document.getElementById("rezultat");let dugme = document.getElementById("btn");let god = document.getElementById("godine");function myFunction() {&nbsp; &nbsp;let text = '';&nbsp; &nbsp;if (god >= 18){&nbsp; &nbsp; &nbsp;text = "Uspesno ste se prijavili";&nbsp; &nbsp; } else if (god <= 65){&nbsp; &nbsp; text = "Uspesno ste se prijavili";&nbsp; &nbsp; } else if (god < 18){&nbsp; &nbsp; text = "Imate premalo godina";&nbsp; &nbsp; }else if (god > 65){&nbsp; &nbsp; text = "Imate previse godina";&nbsp; &nbsp;}&nbsp; document.getElementById("rezultat").innerHTML = text;}

摇曳的蔷薇

问题是测试是针对变量“god”进行的,它实际上是 HTML 输入元素,而不是用户输入的值。要获得值,您需要使用 god.value尝试这个let = document.getElementById("rezultat");//BTW this does nothing usefullet dugme = document.getElementById("btn");let god = document.getElementById("godine");function myFunction() {if (god.value >= 18){&nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";}if (god.value <= 65){&nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";}if (god.value < 18){&nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Imate premalo godina";}if (god.value > 65){&nbsp; &nbsp; document.getElementById("rezultat").innerHTML = "Imate previse godina";}}<p id="rezultat"></p><input id="godine"/><button id="btn" onclick="myFunction();">Submit</button>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答