开发者

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜