获取已禁用选项的选项值

时间:2018-03-09 10:22:43

标签: javascript jquery html

我在select标签中禁用了一个选项

       <div class="form-group required">
          <label class="col-sm-3 control-label">Status</label>
            <div class="col-sm-4">
             <select class="form-control validate-required" id="employee_status">
           <option value="">Select Status</option>
           <option value="Reinitiated" disabled="">Reinitiated</option>
           <option value="Suspended">Suspended</option>
           <option value="Terminated">Terminated</option>
           <option value="Resigned">Resigned</option></select>
   <small id="employee_status_help" class="text-danger hide">Status is required</small>
    </div>
 </div>

我尝试过以下方法来获取已禁用选项的值

          inputvalue = $('#employee_status option[disabled]:selected').val();

它不起作用......

3 个答案:

答案 0 :(得分:4)

这是因为未选择禁用选项。请删除:selected

&#13;
&#13;
var inputvalue = $('#employee_status option[disabled]').val();

console.log(inputvalue);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group required">
  <label class="col-sm-3 control-label">Status</label>
  <div class="col-sm-4">
    <select class="form-control validate-required" id="employee_status">
           <option value="">Select Status</option>
           <option value="Reinitiated" disabled="">Reinitiated</option>
           <option value="Suspended">Suspended</option>
           <option value="Terminated">Terminated</option>
           <option value="Resigned">Resigned</option></select>
    <small id="employee_status_help" class="text-danger hide">Status is required</small>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

disabled选项不能是selected

你只需要这个:

$('#employee_status option[disabled]').val();

&#13;
&#13;
console.log($('#employee_status option[disabled]').val());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group required">
  <label class="col-sm-3 control-label">Status</label>
  <div class="col-sm-4">
    <select class="form-control validate-required" id="employee_status">
           <option value="">Select Status</option>
           <option value="Reinitiated" disabled="">Reinitiated</option>
           <option value="Suspended">Suspended</option>
           <option value="Terminated">Terminated</option>
           <option value="Resigned">Resigned</option></select>
    <small id="employee_status_help" class="text-danger hide">Status is required</small>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

尝试使用inputvalue = $('#employee_status option[disabled]').val();它应该诀窍

我已经尝试过这样的事情了,似乎&#39;:已选择&#39;无效,因此返回undefined

您可能想要使用::selection但是会​​选择用户选择的元素部分

您可以看到我的代码

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    inputvalue = $('#employee_status option[disabled]').val();
    console.log(inputvalue);
});
</script>
</head>
<body>

<div class="form-group required">
          <label class="col-sm-3 control-label">Status</label>
            <div class="col-sm-4">
             <select class="form-control validate-required" id="employee_status">
           <option value="">Select Status</option>
           <option value="Reinitiated" disabled="">Reinitiated</option>
           <option value="Suspended">Suspended</option>
           <option value="Terminated">Terminated</option>
           <option value="Resigned">Resigned</option></select>
   <small id="employee_status_help" class="text-danger hide">Status is required</small>
                      </div>
                  </div>

</body>
</html>