javascript - 在 Select2 中选择选项 - 禁用/启用操作未触发

标签 javascript jquery forms jquery-select2

我有一个选择 Select2 下拉框和旁边的空白文本框,默认情况下处于禁用状态。

当用户选择一个选项“ServiceNow”时,我希望将文本框设置为 false。

目前,该功能似乎是随机的。四个可用选项中的两个启用该字段,两个禁用它。即使是我想要的那个也无法实现它。我在这里做错了什么?

表单 HTML

<div>
  <label for="feature">Feature</label>
  <select class="form-control select2" style="width: 100%;" name="feature" id="feature">
    <option selected="selected" disabled="disabled">-Select-</option>
    <option name="ServiceNow" value="ServiceNow">Service Now</option>
    <option>Epic Upgrade</option>
    <option>Alarais Interop</option>
    <option>Pyxis Upgrade</option>
  </select>
</div>
<div class="col-xs-4">
  <label for="serviceNowID">ServiceNow ID</label>
  <input type="text" class="form-control" name="serviceNowID" id="serviceNowID" disabled="disabled" placeholder="ServiceNow ID (This doesn't work yet.)">
</div>

相关JS

<script> 
//Get the ServiceNow thing to unlock on option select
//Evaluate if option is selected
$('#feature').on("select2:selecting", function(e) { 
   var choice = $(this).find('option').val();

   if ($(this).val() == 'ServiceNow') {
    document.getElementById("serviceNowID").disabled = false;
   } else {
    document.getElementById("serviceNowID").disabled = true;
   }
});
</script>

谢谢!

最佳答案

When the user selects an option, namely "ServiceNow" I would like the text box to set disabled to false.

也许您应该使用“选择”事件而不是“选择”?

$('#feature').on("select2:select", function(e) { 
...

关于javascript - 在 Select2 中选择选项 - 禁用/启用操作未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47042464/

相关文章:

javascript - 当指令 Controller 使用 Angular 读取它时,指令链接中的范围变量不会更新

javascript - jquery 按钮名称更改不起作用

javascript - 如何修复 Cordova/Phonegap 中的 iFrame 问题 (iOS)?

javascript - jquery 通过文本框值自动完成

javascript - Ajax成功函数无法在自定义函数内访问

php - 向程序员寻求帮助

javascript - Javascript自定义视频播放器当前时间/总时间

javascript - 提交后重置输入字段

PHP Form 不向 mySQL 数据库添加数据?

php - 使用 javascript、PHP、MySQL 进行 html 表单处理时的安全预防措施