鉴于以下内容,我希望使用此功能(回发)在后续排序中保留当前选定的项目。当前行为是选择列表中的最后一个排序项。
$(document).ready(function () {
$('select').each(function () {
sortDropDownListByText(this);
});
});
// pass the select object to a function to sort
function sortDropDownListByText(selectObject) {
$(selectObject).html($("option", selectObject).sort(function (a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
}));
}
答案 0 :(得分:2)
// pass the select object to a function to sort
function sortDropDownListByText(selectObject) {
var selectedValue = $(selectObject).val();
$(selectObject).html($("option", selectObject).sort(function (a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
})).val(selectedValue);
}