简体   繁体   中英

How to change order in dropdown to replace item

Is it possible to change the order in the dropdown to replace an item?

在此处输入图片说明

I need to change it to alphabetic order. Any idea?

Thanks in advance!

Please find below snippet to sort selectlist

 $("#mSelect").append($("#mSelect option").remove().sort(function(a, b) { var at = $(a).text(), bt = $(b).text(); return (at > bt)?1:((at < bt)?-1:0); })); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="mSelect" > <option value="val1" > DEF </option> <option value="val4" > GRT </option> <option value="val2" > ABC </option> <option value="val3" > OPL </option> <option value="val5" > AWS </option> <option value="val9" > BTY </option> </select> 

Small Correction: Added last line for clearing default option selection

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="mSelect" >
    <option value="val1" > DEF </option>
    <option value="val4" > GRT </option>
    <option value="val2" > ABC </option>
    <option value="val3" > OPL </option>
    <option value="val5" > AWS </option>
    <option value="val9" > BTY </option>
</select>

$("#mSelect").append($("#mSelect option").remove().sort(function(a, b) {
    var at = $(a).text(), bt = $(b).text();
    return (at > bt)?1:((at < bt)?-1:0);
}));

$("#mSelect").val("");

At the moment there is no such option. A jQuery-Hacky method may be possible, but I wouldn't try it. Basically if you want this, you should create an issue on github, but you'll probably have to contribute the code :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM