我有一个jQuery移动自定义多选,但当我选择一个项目时,我们会在HTML select
标记上显示项目列表,但不会使用selected
属性进行更新。
Using the Multiple selects example of the page:
<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>
是否可以默认添加selected
属性?
答案 0 :(得分:1)
默认情况下,JQuery Mobile不会更新selected
属性以响应用户的输入。您可以使用以下代码使所选值与属性保持同步。
$(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');
}
}
});
});
请记住,这适用于DOM中已有的所有selectmenu小部件。如果以编程方式添加一个,则还必须将事件处理程序添加到该窗口小部件。