0

I have an option-select HTML thing. The JS code:

var inviteds = $('#clientMenuEvents_edit select[name="everybody"]');
inviteds.find('option').sort (function(a,b) {
    return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;
});

I debugged the function, a.innerHTML.toLowerCase() gives the string, but it just can't order that list, nothing happens. Everybody does it like that - but it acts like I didn't do anything.

4

1 回答 1

4

您正在对元素进行排序,但排序只影响元素集,而不影响 DOM。如果您使用.append()将集合放回选择元素,它们将按排序顺序添加。

inviteds.find('option').sort (function(a,b) {
    return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;
}).appendTo( inviteds );

演示:http: //jsfiddle.net/CAZCf/1/

于 2013-06-28T15:42:54.830 回答