0

我不想调用两次 $('#selecteShuffle')

function sortSelect(select) {
  select.html($('select option').sort(function(a, b){
      return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
  }));
}

$('#selectShuffle').change(sortSelect($('#selectShuffle')));
4

1 回答 1

2

像这样做 -

$('#selectShuffle').change(sortSelect);

并在您的功能中-this参考您的选择

function sortSelect(e) {
  $(this).html($('select option').sort(function(a, b){
      return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
  }));
}

或者你也可以这样做 - (不改变你当前的功能设置

function sortSelect(select) {
  select.html($('select option').sort(function(a, b){
      return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
  }));
}

$('#selectShuffle').change(function(){
   sortSelect($(this))
});
于 2013-06-21T16:29:19.870 回答