You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BMI计算器按钮无响应:代码无法执行验证与条件判断问题

解决BMI计算器Submit按钮无响应问题

核心问题(导致按钮无反应的直接原因)

你的JavaScript代码存在语法错误,浏览器无法正常解析执行点击事件逻辑:

  • 第四个else if分支的条件写法错误:bmi<35 && bmi >=29.9> 末尾多了一个多余的>符号,直接触发语法解析失败。
  • 所有else if代码块末尾使用了};,多余的分号会提前终止语句,导致后续分支逻辑无法被识别。

额外问题(影响计算准确性)

  1. HTML英寸输入框配置错误:max=".11" 不符合实际逻辑(英寸范围应为0-11),且step=".01"会让用户输入小数,实际应输入整数英寸,建议修正为:
    <input type="number" id="inches" min="0" max="11" step="1" required><label for="inches"> Inches</label>
    
  2. 身高计算逻辑错误:原代码中heightInches * 10无法正确转换英寸数值,修正HTML输入框后,直接使用height = (heightFeet * 12) + heightInches即可得到总英寸数。

修复后的完整JavaScript代码

//[weight (lb) / height (in) / height (in)] x 703

document.querySelector("#submit").addEventListener("click", function (e) {
    if (document.querySelector("#feet").reportValidity() &&
        document.querySelector("#inches").reportValidity() &&
        document.querySelector("#weight").reportValidity()) {
        let heightFeet = parseFloat(document.querySelector("#feet").value);
        let heightInches = parseFloat(document.querySelector("#inches").value);
        // 修正身高计算:转换为总英寸数
        let height = (heightFeet * 12) + heightInches;
        let weight = parseFloat(document.querySelector("#weight").value);
        let bmi = (weight / height / height) * 703;
        // 保留两位小数,优化显示效果
        bmi = bmi.toFixed(2);

        if (bmi < 18.5) {
            document.querySelector("#finalResult").innerHTML = `Your BMI is ${bmi}, indicating your weight is in the UNDER WEIGHT RANGE for adults of your height.`
            document.querySelector("#img").src = "/img/underWeight.jpg";
        }
        else if(bmi < 24.9 && bmi >= 18.5){
            document.querySelector("#finalResult").innerHTML = `Your BMI is ${bmi}, indicating your weight is in the HEALTHY WEIGHT RANGE for adults of your height.`
            document.querySelector("#img").src = "/img/healthyWeight.jpg";
        }
        else if(bmi < 29.9 && bmi >= 24.9){
            document.querySelector("#finalResult").innerHTML = `Your BMI is ${bmi}, indicating your weight is in the OVER WEIGHT RANGE for adults of your height.`
            document.querySelector("#img").src = "/img/overWeight.jpg";
        }
        else if(bmi < 35 && bmi >= 29.9){
            document.querySelector("#finalResult").innerHTML = `Your BMI is ${bmi}, indicating your weight is in the OBESE WEIGHT RANGE for adults of your height.`
            document.querySelector("#img").src = "/img/obeseWeight.jpg";
        }
        else if(bmi >= 35){
            document.querySelector("#finalResult").innerHTML = `Your BMI is ${bmi}, indicating your weight is in the EXTREME OBESE WEIGHT RANGE for adults of your height.`
            document.querySelector("#img").src = "/img/extremeObeseWeight.jpg";
        }
        else{
            alert("ERROR!")
        }
    }
});

内容的提问来源于stack exchange,提问作者Danna94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 20:05:31