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

JavaScript下拉菜单切换后elevatorCalc()函数运行失效问题求助

问题原因
  • checked属性仅适用于单选框、复选框这类表单控件,<select>下拉菜单的<option>元素不存在该属性,因此你原代码中所有的判断条件都会返回false,无法触发对应分支的逻辑。
  • 额外补充一个隐藏问题:你写的commercial选项的value值末尾多了一个多余空格,后续判断时要注意对齐,避免匹配失败。
修复方案
  1. 先修正HTML中commercial选项的多余空格:
<!-- 原来的错误写法,value末尾多空格 -->
<option id="commercial" value="commercial " [...]>Commercial</option>
<!-- 修改后 -->
<option id="commercial" value="commercial" [...]>Commercial</option>
  1. 修改elevatorCalc()函数的判断逻辑,改为读取下拉菜单的选中值做判断:
function elevatorCalc() {
  const floors = document.getElementById("resinput_number-of-floors");
  // 新增:获取下拉菜单的当前选中值
  const selectedType = document.getElementById("building-type").value;

  if (selectedType === "commercial") {
    commercial(); 
  } else if (selectedType === "residential" && parseInt(floors.value) <= 20) {
    residential();
  } else if (selectedType === "residential" && parseInt(floors.value) > 20) {
    residential20();
  } else if (selectedType === "corporate") {
    corpo();
  } else if (selectedType === "hybrid") {
    hybrid();
  }   
}
  1. (可选优化)给下拉菜单本身也绑定onchange事件,切换建筑类型时可直接触发计算,提升使用体验:
<select id='building-type' onchange="elevatorCalc()">
  <!-- 原有option内容 -->
</select>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:09:03