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

如何用JavaScript实现下拉菜单选Other时唤起文本框?代码故障排查

解决下拉菜单选中Other时显示文本框的问题

你的代码存在两个关键问题:

  • 错误地获取了select元素(id为issue)进行显示/隐藏操作,导致选中非Other选项时整个下拉菜单被隐藏,这就是你看到的“剩余选项消失”的原因。
  • 文本框容器(id为textbox)内没有实际的输入控件,且初始未设置隐藏状态,导致选中Other时看不到文本框。

修正后的完整代码如下:

HTML 部分

<select name="issue" id="issue" onchange='disappear(this.value)'>
  <option disabled selected></option>
  <option value="bug">Bug</option>
  <option value="lacking">Lack of Use</option>
  <option value="other">Other</option>
</select>
<div id="textbox" style="display:none;">
  <input type="text" placeholder="请输入其他问题描述">
</div>

JavaScript 部分

function disappear(val) {
  var textbox = document.getElementById('textbox');
  if (val == 'other') {
    textbox.style.display = 'block';
  } else {
    textbox.style.display = 'none';
  }
}

核心修改点:

  • JS中改为获取id为textbox的容器元素,而非select元素。
  • 在textbox容器内添加实际的文本输入框,并设置初始display:none隐藏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 04:45:41