首页 > 解决方案 > 无法使选择禁用和启用

问题描述

当我分别单击编辑和保存按钮时,我想使文本和选择选项禁用和启用。在我的情况下,文本字段在点击时被禁用和启用,但选择下拉菜单没有被禁用和启用。在 javascript 中,我调用了该类来禁用和启用文本字段。如何进行选择。

$(document).ready(function() {
  $('.editBtn').click(function() {
    if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
      $('.editField').prop('readonly', false); //turns the readonly off
      $('.editBtn').html('<span class="glyphicon glyphicon-floppy-disk">&nbsp;</span>'); //Changes the text of the button
    } else { //else we do other things
      $('.editField').prop('readonly', true);
      $('.editBtn').html('<span class="glyphicon glyphicon-pencil">&nbsp;</span>');
    }
  });
});

HTML:

<select id="dept" class="form-control editField" required></select>

标签: javascriptjqueryhtml

解决方案


由于您使用的是 jQuery,因此您可以这样做来启用选择:

$('.mySelect').prop('disabled', false); //enabled

这可以禁用选择:

$('.mySelect').prop('disabled', true); //disabled

推荐阅读