Get the value of the parameter "Disabled" in "Select multiple jQuery"

I have multiple selections on my page and I have disabled the option so that the user cannot cancel them, but I cannot figure out how to get the value of the disabled option.

My code is bye

// Get selected positions var $selPositions = $('select#empPositions').val(); 

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 that the disabled option changes based on other variables, but it only gives me the selected values ​​without disabling. Can someone tell me if this can be done and how?

I use Chosen , so the disabled parameter

Fiddle: http://jsfiddle.net/c5kn5w75/

I found this article on the jQuery Bug site that said

The long-term logic in .val () ensures that we do not return disabled options in select-multiple. This change simply applies the same behavior for select-one for consistency. You can go to the disabled option value via $ ("select"). Prop ("selectedIndex") if you need it.

But that did not work for me.

+6
source share
2 answers

Demo

 var opt = $('#empPositions option:selected').map(function(i,v) { return this.value; }).get(); // result is array alert( opt ); 

Please keep in mind that when submitting a form, disabled parameters will not be sent even if they are selected. If you send via AJAX, you can get the values ​​as shown above.

Also note that $('option[disabled]').val() will return the value of the first disabled option element and $('option[disabled]:selected').val() value of the first selected disabled option.

If there is always only one element of the selected disabled option, you can configure it using:

  var opt = $('#empPositions option[disabled]:selected').val(); // result is string 
+9
source

You can get the value of a disabled item by doing something like this:

 $('option[disabled]').val(); 
0
source

Source: https://habr.com/ru/post/975006/


All Articles