3 回答

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

TA贡献1880条经验 获得超4个赞
let = document.getElementById("rezultat");
let dugme = document.getElementById("btn");
let god = document.getElementById("godine");
function myFunction() {
let text = '';
if (god >= 18){
text = "Uspesno ste se prijavili";
} else if (god <= 65){
text = "Uspesno ste se prijavili";
} else if (god < 18){
text = "Imate premalo godina";
}else if (god > 65){
text = "Imate previse godina";
}
document.getElementById("rezultat").innerHTML = text;
}

TA贡献1793条经验 获得超6个赞
问题是测试是针对变量“god”进行的,它实际上是 HTML 输入元素,而不是用户输入的值。
要获得值,您需要使用 god.value
尝试这个
let = document.getElementById("rezultat");//BTW this does nothing useful
let dugme = document.getElementById("btn");
let god = document.getElementById("godine");
function myFunction() {
if (god.value >= 18){
document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";
}
if (god.value <= 65){
document.getElementById("rezultat").innerHTML = "Uspesno ste se prijavili";
}
if (god.value < 18){
document.getElementById("rezultat").innerHTML = "Imate premalo godina";
}
if (god.value > 65){
document.getElementById("rezultat").innerHTML = "Imate previse godina";
}
}
<p id="rezultat"></p>
<input id="godine"/>
<button id="btn" onclick="myFunction();">Submit</button>
添加回答
举报