开发者

Keep Selected item when using js to sort

开发者 https://www.devze.com 2023-04-11 21:02 出处:网络
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 select

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

精彩评论

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

关注公众号