JavaScriptの2つの条件を確認するのに問題があります。私が数字を入れると、最初のif
の声明が働くでしょう。ただし、else if
ステートメントは、その条件に従ったものは表示しません。助けていただければ幸いです。他の場合2つの異なる条件の数値を比較するためのステートメント
<html>
<head>
<script>
function return() {
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var x = num1.value;
var y = num2.value
if (isNaN(x)) {
alert("Please only enter numbers");
return false;
}
if (isNaN(y)) {
alert("Please only enter numbers");
return false;
}
var calculation = (x + y)/(x * y);
var result = document.getElementById("answer");
if (calculation > 1) {
result.value = "Your number is more than 1";
} else {
if (calculation <= 0.5) {
result.value = "Your number is 1 or less";
}
}
}
</script>
</head>
<body>
1: <input type="text" id="num1"/>
2: <input type="text" id="num2"/>
<input type="button" value="Calculate" onclick= "return()"/>
<input type="text" id="answer"/>
</body>
</html>
結果は '0.5'と '1'の間にある場合はどう?条件は満たされず、何も表示されません。 – Tushar
'var計算=(+ x + + y)/(x * y);' – Rayon
'return'を関数名にすることはできません。 – Rayon