“请输入一个值”当所有输入字段都填满时

时间:2018-08-16 01:19:11

标签: javascript

当我在两个框中都输入一个值时,仍然出现错误“请输入一个值”。如果我注释掉第二条If语句,则它可以正常工作,只是不验证保留为空的字段。不确定if语句出了什么问题。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="styles.css">
    <script src="bmi.js"></script>
    <title>BMI Calculator</title>
</head>

<body class="whole">
    <h2>BMI Calculator!</h2>
    <form>
        <section id="whinputs" class="inputs">
            <input id="weight" type="text" placeholder="Enter weight in pounds">
            <input id="height" type="text" placeholder="Enter height in inches">
        </section>

        <section class="buttons">
            <input type="button" onclick="valid()" value="Calculate BMI">
            <input type="reset">
        </section>
    </form>
    <h2 id="resultline"></h2>



</body>

</html>

JS:

var valid = function () {
    var weight = document.getElementById('weight').value;
    var height = document.getElementById('height').value;


    if (isNaN(weight || height)) {
        return alert("Value must be a number!");
    }
    if (weight || height === "") {
        return alert("Please enter a value");
    }
    else {
        var result = ((weight / (Math.pow(height, 2))) * 703);
        var result = parseFloat(result).toFixed(2)
        // return alert("Your BMI is " + result)
        return document.getElementById('resultline').innerHTML = ("Your BMI is " + result);
    }
}

4 个答案:

答案 0 :(得分:1)

您的代码中有两个问题。一种是您提出的问题,另一种可能尚未注意到。

您提出的问题

问题出在以下代码中:

path2

上面的代码翻译为:

local path2, occurrences = removeHtmlExtensions(lol2)

print(occurrences) -- 2

但这不是您真正想要的。相反,您应该这样做:

if (weight || height === "") {
    return alert("Please enter a value");
}

翻译为:

if [weight is a truthy value] OR [height is an empty string]
then return alert('Please enter a value');

您可能尚未注意到的问题

if (weight === '' || height === ''){
    return alert('Please enter a value');
}

翻译为:

if [weight is an empty string] OR [height is an empty string]
then return alert('Please enter a value');

但这不是您真正想要的。相反,您应该这样做:

if (isNaN(weight || height)) {
    return alert("Value must be a number!");
}

翻译为:

if weight is a truthy value, then use isNaN(weight), else use isNaN(height)
then return alert('Value must be a number!');

测试代码

if (isNaN(weight) || isNaN(height)){
    return alert('Value must be a number!');
}
if ( weight is NaN OR height is NaN )
then return alert('Value must be a number!');

答案 1 :(得分:0)

问题是您的if语句的逻辑。试试这个:

if ((weight === "") || (height === "")) {
    return alert("Please enter a value");
}

答案 2 :(得分:0)

第二个if语句错误

if (weight || height === "")
如果输入了重量,则

为true 您想要if (isNaN(weight) || isNaN(height))之类的东西,如果要确保输入为数字,则可能还需要检查输入是否被0除

答案 3 :(得分:0)

您正在检查体重是否是通过离开来定义的。

if (isNaN(weight || height)) {
        return alert("Value must be a number!");
    }
    if (weight == "" || height == "") {
        return alert("Please enter a value");
    }
    else {
        var result = ((weight / (Math.pow(height, 2))) * 703);
         result = parseFloat(result).toFixed(2)
        // return alert("Your BMI is " + result)
        document.getElementById('resultline').innerHTML = ("Your BMI is " + result);