我不知道我在MaxN函数上做错了什么。最大数量显示正确。但是,如果我必须显示警报消息以防用户未输入任何数字,它将显示在警报“ NaN”中,而不是必需的消息。 这是我的代码:
JS文件:
function getFirstNumber()
{
var value1 = document.getElementById("fnumber").value;
var fn = parseInt(value1);
return fn;
}
function getSecondNumber()
{
var value2 = document.getElementById("snumber").value;
var sn = parseInt(value2);
return sn;
}
function getThirdNumber()
{
var value3 = document.getElementById("tnumber").value;
var tn = parseInt(value3);
return tn;
}
function MaxN()
{
var fn = getFirstNumber();
var sn = getSecondNumber();
var tn = getThirdNumber();
if (fn == "" && sn == "" && tn =="") {
alert('Please input at least one number');
} else if (fn >= sn && fn >= tn){
alert(fn);
} else if (sn >= fn && sn >= tn){
alert(sn);
} else {
alert(tn);
}
}
HTML文件:
<html>
<head>
<script type="text/javascript" src="javafile_exercise.js"></script>
</head>
<body>
<div>
<span>First number: </span><input type="text" id="fnumber" />
</div>
<div>
<span>Second number: </span><input type="text" id="snumber" />
</div>
<div>
<span>Third number: </span><input type="text" id="tnumber" />
</div>
<br>
<button type="button" onclick="MaxN()">Max Number</button>
</br>
</body>
</html>
答案 0 :(得分:1)
仅在
时显示消息 fn == "" and sn == "" and tn ==""
这意味着所有三个文本字段都必须为空。使用OR语句,应该可以解决您的问题。
如果文本字段之一为空,则比您的消息应该显示。
此刻,您正在调用该函数,即使on为空,因此其值为i NaN。
修改修正:
if ((fn == "") || (sn == "") || (tn ==""))
{
alert('One of the text field is empty');
}
另一种解决方法,当文本字段为空时返回0。这样,即使文本字段为空,您也将始终有一个数字。
var value1 = document.getElementById("fnumber").value;
if (value1 == null)
{
return 0;
}
else
{
var fn = parseInt(value1);
return fn;
}
答案 1 :(得分:0)
如果您更改逻辑,当里面没有写东西的时候,输入值是不是数字,它将起作用,因此您应该使用isNaN
。
function getFirstNumber()
{
var value1 = document.getElementById("fnumber").value;
var fn = parseInt(value1);
return fn;
}
function getSecondNumber()
{
var value2 = document.getElementById("snumber").value;
var sn = parseInt(value2);
return sn;
}
function getThirdNumber()
{
var value3 = document.getElementById("tnumber").value;
var tn = parseInt(value3);
return tn;
}
function MaxN()
{
var fn = getFirstNumber();
var sn = getSecondNumber();
var tn = getThirdNumber();
if (isNaN(fn) && isNaN(sn) && isNaN(tn)) {
alert('Please input at least one number');
} else if (fn >= sn && fn >= tn){
alert(fn);
} else if (sn >= fn && sn >= tn){
alert(sn);
} else {
alert(tn);
}
}
<div>
<span>First number: </span><input type="text" id="fnumber" />
</div>
<div>
<span>Second number: </span><input type="text" id="snumber" />
</div>
<div>
<span>Third number: </span><input type="text" id="tnumber" />
</div>
<br>
<button type="button" onclick="MaxN()">Max Number</button>
</br>
编辑,通过在函数开始时将NaN
设为0,使用另一种逻辑
function getFirstNumber()
{
var value1 = document.getElementById("fnumber").value;
var fn = parseInt(value1);
return fn;
}
function getSecondNumber()
{
var value2 = document.getElementById("snumber").value;
var sn = parseInt(value2);
return sn;
}
function getThirdNumber()
{
var value3 = document.getElementById("tnumber").value;
var tn = parseInt(value3);
return tn;
}
function MaxN()
{
var fn = isNaN(getFirstNumber()) ? 0 : getFirstNumber() ;
var sn = isNaN(getSecondNumber()) ? 0 : getSecondNumber() ;
var tn = isNaN(getThirdNumber()) ? 0 : getThirdNumber() ;
if (fn == 0 && sn == 0 && tn == 0) {
alert('Please input at least one number');
} else if (fn >= sn && fn >= tn){
alert(fn);
} else if (sn >= fn && sn >= tn){
alert(sn);
} else {
alert(tn);
}
}
<div>
<span>First number: </span><input type="text" id="fnumber" />
</div>
<div>
<span>Second number: </span><input type="text" id="snumber" />
</div>
<div>
<span>Third number: </span><input type="text" id="tnumber" />
</div>
<br>
<button type="button" onclick="MaxN()">Max Number</button>
</br>
最后但并非最不重要。 您应该做的是。
number
,例如<input type="number" min="0"
max="1000" step="1" />
checkValidity()
,setCustomValidity()
)答案 2 :(得分:0)
这是您的意思吗?
function getFirstNumber()
{
var value1 = document.getElementById("fnumber").value;
var fn = parseInt(value1);
return fn;
}
function getSecondNumber()
{
var value2 = document.getElementById("snumber").value;
var sn = parseInt(value2);
return sn;
}
function getThirdNumber()
{
var value3 = document.getElementById("tnumber").value;
var tn = parseInt(value3);
return tn;
}
function MaxN()
{
var fn = getFirstNumber();
var sn = getSecondNumber();
var tn = getThirdNumber();
if(!isNaN(fn)&&!isNaN(sn)&&!isNaN(tn)){
if (fn == "" && sn == "" && tn =="") {
alert('Please input at least one number');
} else if (fn >= sn && fn >= tn){
alert(fn);
} else if (sn >= fn && sn >= tn){
alert(sn);
} else {
alert(tn);
}
}
else
alert("Please Enter Numbers Only!!");
}
<html>
<head>
<script type="text/javascript" src="javafile_exercise.js"></script>
</head>
<body>
<div>
<span>First number: </span><input type="text" id="fnumber" />
</div>
<div>
<span>Second number: </span><input type="text" id="snumber" />
</div>
<div>
<span>Third number: </span><input type="text" id="tnumber" />
</div>
<br>
<button type="button" onclick="MaxN()">Max Number</button>
</br>
</body>
</html>
答案 3 :(得分:0)
这样的事情怎么样...
function getNumber(id, number, ERRMessage) {
var result = false;
var elm = document.getElementById(id);
try {
var number = parseInt(value2);
var result = true;
} catch (ex) {
ERRMessage = ex.message;
}
return result;
}
function MaxN(s) {
//create id array
var ids = s.split(" ");
//init result array
var numbers = []
//get the numbers from the form
for (var i = 0; i < s.length; i++) {
if (getNumber(ids[i], number,ERR)) {
numbers.push(number)
} else {
console.log(ERR);
}
}
// do we have 3 numbers at least
if (numbers.length < 3) { // maybee not
alert(tn);
return;
} else {// we have 3 numbers
alert("Yippiee"); // get exited
console.log(numbers)
// get the maximum of the numbers
var max = numberts[0];
for (var i = 0; i < s.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
alert("MAXIMUM is" + max) // show result
}
}
健壮,可重用和灵活,只需在函数中输入输入字段的ID。它还将显示我猜不到的3个数字中的最大值。 示例调用:
var s = "fnumber snumber tnumber";
MaxN(s);