我有以下代码在下拉列表中对项目进行排序:
- function sortDropDownListByText(selectId) {
- $(selectId).html($(selectId + " option").sort(function(a,b) {
- return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
- }))
- }
这样做很好,除非在我的第一个项目中我有一个**“请从列表中选择和项目”消息. . **
有没有办法我可以排序选择列表中的项目,并保持“请选择条目”作为列表中的第一个项目?
编辑:
响应一些答案,“请选择项始终有值0”
解决方法
- function sortDropDownListByText(selectId) {
- var foption = $('#'+ selectId + ' option:first');
- var soptions = $('#'+ selectId + ' option:not(:first)').sort(function(a,b) {
- return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
- });
- $('#' + selectId).html(soptions).prepend(foption);
- };
是你的功能