Skip to content

Instantly share code, notes, and snippets.

@lostsnow
Forked from boy3vil/sorting option select jQuery
Last active January 2, 2016 19:19
Show Gist options
  • Save lostsnow/8349367 to your computer and use it in GitHub Desktop.
Save lostsnow/8349367 to your computer and use it in GitHub Desktop.
add chinese char support
<html>
<head>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function() {
var options = $('select.whatever option');
var arr = options.map(function(_, o) {
var os = $(o).attr("selected") || false;
return {
t: $(o).text(),
v: $(o).attr("value"),
s: os
};
}).get();
arr.sort(function(o1, o2) {
return o1.t.localeCompare(o2.t);
});
options.each(function(i, o) {
console.log(i);
$(o).attr("value", arr[i].v);
$(o).text(arr[i].t);
$(o).attr("selected", arr[i].s);
});
});
});
</script>
</head>
<body>
<button type=button>Sort Options</button>
<select class='whatever'>
<option value='22'>咋咋咋</option>
<option value='101'>哈哈</option>
<option value='1'>医院</option>
<option value='-2'>嗷嗷</option>
</select>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment