0

我有一个表格,我想按字母顺序对下拉列表和复选框字段进行排序。我尝试使用 javascript 但它不工作。

    $(function() {
    var select = $('select');
    select.html(select.find('option').sort(function(x, y) {
    return $(x).text() > $(y).text() ? 1 : -1;
    }));

    // $('select').get(0).selectedIndex = 0;
    });

谢谢您的帮助

4

1 回答 1

1

看来我们在这里有了答案:点击

我将复制其中的一部分:

var options = $('select option');
var arr = options.map(function(_, o) { return { t: $(o).text(), v: o.value }; }).get();
arr.sort(function(o1, o2) { return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0; });
options.each(function(i, o) {
  o.value = arr[i].v;
  $(o).text(arr[i].t);
});
于 2020-08-26T20:44:49.743 回答