Sort Options in a Select List with javascript/jQuery.. but not alphabetically

前端 未结 4 875
猫巷女王i
猫巷女王i 2021-02-05 10:23

I am looking for a function that will take a list of options in a select list and sort them alphabetically but with a twist. All values with the text \'NA\' should be pushed to

4条回答
  •  别那么骄傲
    2021-02-05 10:53

    I've made ​​some changes to the original function and it worked fine.

    function NASort(a, b) {  
        console.log(a.innerHTML.substr(0, 1).toLowerCase() + ' > ' + b.innerHTML.substr(0, 1).toLowerCase());
        return (a.innerHTML.substr(0, 1).toLowerCase() > b.innerHTML.substr(0, 1).toLowerCase()) ? 1 : -1;
    };
    
    $('select option').sort(NASort).appendTo('select');
    

提交回复
热议问题