我在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();
它不起作用......
答案 0 :(得分:4)
这是因为未选择禁用选项。请删除:selected
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;
答案 1 :(得分:1)
但disabled
选项不能是selected
你只需要这个:
$('#employee_status option[disabled]').val();
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;
答案 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>