Keep Selected item when using js to sort
Given the following I would like to keep the currently selected item selected on subsequent sorting using this funcion (postbacks). Current behaviour is that the last sorted item in the list is selected.
$(document).ready(function () {
$('select'开发者_StackOverflow社区).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;
}));
}
// 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);
}
精彩评论