JQuery mobile Multiselect does not update the selected attribute

I have jQuery mobile multiplayer for mobile devices, but when I select one element, we have a list of elements in the HTML select tag, but it does not update with the selected attribute.

Use Multiple Select Page Example :

 <div data-role="fieldcontain" class="ui-field-contain ui-body ui-br"> <label for="select-choice-9" class="select ui-select"> Choose shipping method(s): </label> <div class="ui-select"> <a href="#" role="button" aria-haspopup="true" data-theme="c" class="ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-up-c"> <span class="ui-btn-inner ui-btn-corner-all"> <span class="ui-btn-text"> Rush: 3 days, Express: next day </span> <span class="ui-icon ui-icon-arrow-d ui-icon-shadow"></span> </span> <span class="ui-li-count ui-btn-up-c ui-btn-corner-all" style=""> 2 </span> </a> <select name="select-choice-9" id="select-choice-9" multiple="multiple" data-native-menu="false" tabindex="-1"> <option>Choose options</option> <option value="standard">Standard: 7 day</option> <option value="rush">Rush: 3 days</option> <option value="express">Express: next day</option> <option value="overnight">Overnight</option> </select> </div> </div> 

Is there a way to add the selected attribute by default?

+6
source share
1 answer

JQuery Mobile by default will not update the selected attribute in response to user input. You can save the selected values ​​in synchronization with the attributes with the following code.

 $(function () { $('select').on('change', function (evt) { var options = $(evt.target).children('option'), current; for (var i = 0; i < options.length; i++) { current = options[i]; if (current.selected === true && !current.hasAttribute('selected')) { options[i].setAttribute('selected', ''); } if (current.selected === false && current.hasAttribute('selected')) { options[i].removeAttribute('selected'); } } }); }); 

Remember that this will work for all selectmenu widgets that you already have in the DOM. If you add one program code, you also need to add an event handler to this widget.

+1
source

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


All Articles