我无法验证JavaScript的两个条件。当我输入一个数字时,第一个if
语句将起作用。但是,else if
语句不会打印出符合该条件的任何内容。一些帮助将不胜感激。
<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 :(得分:3)
您不能将保留字用作函数名,变量名或标签。
同时将输入值转换为Number
,可以使用Unary plus
。
InputElement.value
返回String
类型的值,因此,如果值为2
和3
,则计算结果为("2" + "3" ==> "23")
,值将连接,不会添加!
另请注意,您的遗失条件未得到处理,您还应该有其他条件。
function calculate() {
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.50) {
result.value = "Your number is 1 or less";
} else {
result.value = "Unhandled condition!";
}
}
}
1: <input type="text" id="num1" /> 2: <input type="text" id="num2" />
<input type="button" value="Calculate" onclick="calculate()" />
<input type="text" id="answer" />
答案 1 :(得分:0)
return is statement,不要将它用作函数名。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
1: <input type="text" id="num1"/>
2: <input type="text" id="num2"/>
<input type="button" value="Calculate" onclick= "returnq()"/>
<input type="text" id="answer"/>
<script>
function returnq(){
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.50) {
result.value = "Your number is 1 or less";
}
}
}
</script>
</body>
</html>
没问题......
抱歉两个答案同时
答案 2 :(得分:0)
else
声明错误尝试删除if (calculation <= 0.50)
,如下所示:
else {
result.value = "Your number is 1 or less";
}
答案 3 :(得分:0)
问题是你return
迫使函数退出,永远不会让第二个if
块有机会运行。此外,return
是JavaScript中的保留关键字,不建议用作函数名称。 Link to jsbin
这里有一些可行的代码:
1:<input type="text" id="num1" /> <br />
2:<input type="text" id="num2" /> <br />
<input type="button" value="Calculate" onclick="sumUpper()" />
<input type="text" id="answer" />
function sumUpper() {
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var x = parseFloat(num1.value);
var y = parseFloat(num2.value);
var xError = false;
var yError = false;
if (isNaN(x)) {
console.log("Please only enter numbers x");
xError = true;
}
if (isNaN(y)) {
console.log("Please only enter numbers y");
var yError = true;
}
console.log(xError, yError);
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.50) {
result.value = "Your number is 1 or less";
}
}
}
答案 4 :(得分:0)
我猜这些数字正在四舍五入,将1和0.5都显示为1.如果必须使用else,请尝试一个限制估算的数学函数。
答案 5 :(得分:0)
您无法使用功能名称return
。 return是javascript关键字。
尝试这样的事情。
function myFunction(){
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.50) {
result.value = "Your number is 1 or less";
}
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1: <input type="text" id="num1"/>
2: <input type="text" id="num2"/>
<input type="button" value="Calculate" onclick= "myFunction()"/>
<input type="text" id="answer"/>
&#13;