我想在转移到另一个列表框时按给定顺序对列表框进行排序

时间:2015-01-20 04:19:53

标签: asp.net-mvc listbox

这是代码

@Html.ListBoxFor(model => model.headerList, new MultiSelectList(Model.headerList), new { id = "AvailableGenres", size = "50", value=1,style = "height: 300px;" })</div>
<input id="btnAddAll" type="button" value=" >> " onclick="addallItems();" />
<input id="btnAdd" type="button" value=" > " onclick="addItem();" />
<input id="btnRemove" type="button" value=" < "  onclick="removeItem();" />
<input id="btnRemoveAll"type="button" value=" << "  onclick="removeallItems();" />
<div style="height:-1px;float:right;">
  @Html.ListBoxFor(model => model.ChosenGenres, new MultiSelectList(Model.ChosenGenres), new { id = "ChosenGenres", @class = "ChosenGenreslist", size = "50", style = "height: 300px;" })
</div>

1 个答案:

答案 0 :(得分:0)

按选项的文字

按字母顺序对第二个列表进行排序
var options = $('#ChosenGenres option');
options.sort(function (x, y) {
  return x.text > y.text;    
});
$('#ChosenGenres').empty().append(options);

附注:建议您学习使用Unobtrusive JavaScript