Javascript 如果选择了特定的下拉选项,显示/隐藏表单行的最佳方式是什么?

Javascript 如果选择了特定的下拉选项,显示/隐藏表单行的最佳方式是什么?,javascript,php,bootstrap-4,Javascript,Php,Bootstrap 4,我有一个表单下拉列表,我的目标是让用户能够选择“其他”,我想做的是在下面显示一个表单行,这样用户就可以输入“其他”选项文本。下面是我的代码,我不确定Bootstrap是否有内置的隐藏/显示功能。如果有帮助的话,我正在使用最新版本。非常感谢 <div class="form-row"> <div class="form-group col-md-6 offset-md-3 mx-auto"> <label class="control-label cust

我有一个表单下拉列表,我的目标是让用户能够选择“其他”,我想做的是在下面显示一个表单行,这样用户就可以输入“其他”选项文本。下面是我的代码,我不确定Bootstrap是否有内置的隐藏/显示功能。如果有帮助的话,我正在使用最新版本。非常感谢

<div class="form-row">
  <div class="form-group col-md-6 offset-md-3 mx-auto">
    <label class="control-label custom_label col-xs-12">Cemetery</label>
    <select type="dropdown" name="cemetery" class="form-control" id="mySelect">
      <option value="select" selected="selected">Select Cemetery</option>
      <option value="AK">Akatarawa</option>
      <option value="TA">Taita</option>
      <option value="WA">Wainuiomata</option>
      <option value="WH">Whenua Tapu</option>
      <option value="MA">Makara</option>
      <option value="KA">Karori</option>
      <option value="ST">St Johns</option>
      <option value="AW">Awa Tapu</option>
      <option value="PA">Paraparaumu</option>
      <option value="other">Other</option>
    </select>
  </div>
</div>

墓地
选择墓地
明泽
泰塔
怀努约马塔
Whenua Tapu
马卡拉
卡洛里
圣约翰斯
阿瓦塔普
帕拉帕劳姆
其他
当选择“其他”选项时,我希望下面的表格行出现,当然,当选择其他下拉选项时,表格行消失

<div class="form-row">
  <div class="form-group col-md-6 offset-md-3 mx-auto">
    <input type="text" id="other" class="form-control" name="cemetery" placeholder="Enter other Cemetery">
  </div>
</div>

我们可以使用引导util类“d-block”和“d-none”来显示/隐藏容器

   function handleSelect() {
       var selected = document.getElementById("mySelect").value;
       var details = document.getElementById("other-details");
       if (selected === "other") {
         details.classList.remove("d-none");
         details.classList.add("d-block");
      } else {
         details.classList.remove("d-block");
         details.classList.add("d-none");
      }
    }

工作

我添加了JQuery选项和vanilla JS选项。我还向option标记添加了一个ID,其值为“other”。只需向该标记添加一个
id=“other”
属性

使用JQuery,您可以使用
.show()
.hide()
在选择其他选项时显示/隐藏输入

var-other=$(“#other”);
var otherInput=$(“.otherInput”);
otherInput.hide();
$('#mySelect')。在('change',function()上{
var值=$(this.val();
如果(值=“其他”){
otherInput.show();
}
});

墓地
选择墓地
明泽
泰塔
怀努约马塔
Whenua Tapu
马卡拉
卡洛里
圣约翰斯
阿瓦塔普
帕拉帕劳姆
其他

您能否显示出不起作用的特定代码段?