option が disabled の時の jQuery の select multiple に対する val の挙動

<select multiple>
  <option disabled selected>bar</option>

上のような select を用いた時に、jQuery で選択中の値を取得しようとすると disabled な option は省かれてしまう。

jquery/val.js at 56136897f241db22560b58c3518578ca1453d5c7 · jquery/jquery · GitHub

bootstrap-select が上手く動かなくて気づいた。

w3c を見ても

The disabled attribute is a boolean attribute. An option element is disabled if its disabled attribute is present or if it is a child of an optgroup element whose disabled attribute is present. An option element that is disabled must prevent any click events that are queued on the user interaction task source from being dispatched on the element.



また、<disabled option element>.selectedtrue を返すし、 <select element>.selectedOptions では disabled のも帰ってくる。


#13097 (using .val() on a disabled html-select-option has different results since 1.8.3) – jQuery - Bug Tracker

javascript - Get Value of Disabled Option in Select Multiple Jquery - Stack Overflow