繁体   English   中英

当文本框最多为3个数字时,JavaScript特定警报为空

[英]javascript specific alert when textbox is empty in maximum of 3 numbers algorithm

我是编码和尝试使用javascript解决算法的新手。 我必须在3个文本框中找到输入的3个数字中的最大值,并对其进行提醒。 我已经做到了,但是当文本框留空而没有输入时,我仍然必须提醒一些消息。 因此,当所有3个字段都保留为空时,我必须生成特定的警报消息,尽管我写了一些东西,但总会得到:“ NaN”警报。 而不是NaN,我想得到:“ Introduceti cel putin un numar”。 你能帮我吗? 这是我的代码(html和javascript)。

谢谢!

<!doctype HTML>
<html>

<head>
    <title>Algoritmi</title>
    <script src="point2.js">

    </script>
</head>

<body>
     <form name="mainForm" style="text-align:center">
        <input type="text" name="textBox1" /> <br/>
        <input type="text" name="textBox2" /> <br/>
        <input type="text" name="textBox3" /> <br/>
        <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" />
    </form>
</body>

</html>

和Javascript:

function calcMax() {
    var a = parseInt(document.mainForm.textBox1.value);
    var b = parseInt(document.mainForm.textBox2.value);
    var c = parseInt(document.mainForm.textBox3.value);
    var max;
    if (a == null || a == "" && b == null || b == "" && c == null || c == "") {
        alert("Introduceti cel putin un numar");
    }
    else {
        if (a > b) {
            if (a > c) {
                max = a
            }
            else {
                max = c;
            }
        }
        else {
            if (b > c) {
                max = b
            }
            else {
                max = c;
            }
        }
        alert(max);
    }
}

当您将空值转换为int时,它将为NaN(不是数字),因此您需要使用isNaN (值)来处理此条件。 如果isNaN的值不是一个数字,否则返回true;否则为false。

在parseInt()步骤之后,如果输入值为空,则a的值将为NaN。 现在我在您的if条件中添加了isNaN(a)条件

 function calcMax() { var a = parseInt(document.mainForm.textBox1.value); var b = parseInt(document.mainForm.textBox2.value); var c = parseInt(document.mainForm.textBox3.value); var max; if ((a == null || a == "" || isNaN(a) ) && (b == null || b == "" || isNaN(b)) && (c == null || c == "" || isNaN(c))) { alert("Introduceti cel putin un numar"); } else { if (a > b) { if (a > c) { max = a } else { max = c; } } else { if (b > c) { max = b } else { max = c; } } alert(max); } } 
 <!doctype HTML> <html> <head> <title>Algoritmi</title> <script src="point2.js"> </script> </head> <body> <form name="mainForm" style="text-align:center"> <input type="text" name="textBox1" /> <br/> <input type="text" name="textBox2" /> <br/> <input type="text" name="textBox3" /> <br/> <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" /> </form> </body> </html> 

注1: &&优先级高于|| ,您应该将||换行 在括号中进行测试。

注意2:您可以检查tha值的length以检查它们是否为空。

注意3:要计算三个值的最大值,您可以简单地使用Math.max

function calcMax() {
    // no need for parseInt
    var a = document.mainForm.textBox1.value;               // you can add .trim() at the end to remove surrounding spaces
    var b = document.mainForm.textBox2.value;               // ...
    var c = document.mainForm.textBox3.value;               // ...
    var max;

    if (a.length * b.length * c.length === 0) {             // at least one of the inputs value's length is 0
        alert("You must fill the inputs");
    }
    else if(isNaN(a) || isNaN(b) || isNaN(c)) {             // at least one of the inputs value is not a valid number
        alert("inputs must be filled with numbers");
    }
    else {
        max = Math.max(a, b, c);                            // a, b and c are all numbers, use Math.max to calculate the maximum (at this stage a, b and c are string, Math.max will internally convert them to numbers)
        alert(max);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM