当选择“其他”选项时显示文本区域

Show textarea when choosing the option "Other.."

本文关键字:显示 文本 区域 选项 选择 其他      更新时间:2023-09-26

我有一个选择项,里面有几个选项,其中一个叫做"other.."。我想要的是,当用户单击"other.."时,将出现一个文本区域,我知道这可以用jQuery完成。下面是我的HTML的简短版本:

<html>
  <body>
    <select>
      <option value="opt1">Option 1</option>
      <option value="opt2">Option 2</option>
      <option value="opt3">Option 3</option>
      <option value="opt4">Option 4</option>
      <option value="other">Other..</option>
    </select>
    <input type="text" id="other_text" />
  </body>
</html>

change事件添加到下拉列表中,如果选择的值为"other",则将切换文本字段:

$('select').on('change', function() {
    $('#other_text').toggle(this.value === 'other');
});