javascript 在选择多个 Jquery 中获取禁用选项的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25734912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Value of Disabled Option in Select Multiple Jquery
提问by dpDesignz
I have a multiple select on my page, and I have an option disabled so that the user can't unselect them, but I can't work out how to get the value of the disabled option.
我的页面上有多个选择,并且我禁用了一个选项,因此用户无法取消选择它们,但我无法弄清楚如何获取禁用选项的值。
My code so far
到目前为止我的代码
// Get selected positions
var $selPositions = $('select#empPositions').val();
HTML
HTML
<select name="empPositions[]" id="empPositions" style="width: 370px;" multiple="" data-placeholder="Choose a Position" required="">
<option></option>
<optgroup label="Admin">
<option disabled="">There are no positions assigned to Admin</option>
</optgroup>
<optgroup label="Information Technology">
<option value="1" selected="" disabled="">IT Developer</option>
<option value="2">IT Geeks</option>
</optgroup>
Note the disabled option changes based on other variables, but it only gives me selected non-disabled values. Can anyone let me know if this can be done and how?
请注意,禁用选项会根据其他变量而更改,但它只为我提供了选定的非禁用值。任何人都可以让我知道这是否可以完成以及如何完成?
I'm using Chosen, hence why the disabled
option
我正在使用Chosen,因此为什么disabled
选择
Fiddle:http://jsfiddle.net/c5kn5w75/
小提琴:http : //jsfiddle.net/c5kn5w75/
I did find this articleon the JQuery Bug Site which said
我确实在 JQuery Bug Site 上找到了这篇文章,其中说
The long-standing logic in .val() ensures that we don't return disabled options in a select-multiple. The change just applies the same behavior for select-one now for consistency. You can get to the disabled option value through $("select").prop("selectedIndex") if you need it.
.val() 中长期存在的逻辑确保我们不会在 select-multiple 中返回禁用的选项。为了保持一致性,更改现在只对 select-one 应用相同的行为。如果需要,您可以通过 $("select").prop("selectedIndex") 获取禁用的选项值。
But that didn't work for me.
但这对我不起作用。
回答by PeterKA
var opt = $('#empPositions option:selected').map(function(i,v) {
return this.value;
}).get(); // result is array
alert( opt );
Please bear in mind that when you submit the form disabled options wont be submitted even if they are selected. If you're submitting via AJAX, then you can grab the values as shown above.
请记住,当您提交表单时,即使选择了禁用选项,也不会提交。如果您通过 AJAX 提交,那么您可以获取如上所示的值。
Also bear in mind that $('option[disabled]').val()
will return the value of the first disabled option
element and $('option[disabled]:selected').val()
the value of the first selected disabled option.
还要记住,这$('option[disabled]').val()
将返回第一个禁用option
元素$('option[disabled]:selected').val()
的值和第一个选定的禁用选项的值。
If there's always just one selected disabled
option element you can target it using:
如果总是只有一个selected disabled
选项元素,您可以使用以下方法定位它:
var opt = $('#empPositions option[disabled]:selected').val(); // result is string
回答by nfen
You can get the value of a disabled element by doing something like this:
您可以通过执行以下操作来获取禁用元素的值:
$('option[disabled]').val();