如何从多选列表框中获取所选项目以便选择它们? [英] How do I get the selected items from a multi-select listbox in order that they were selected?
问题描述
我有一个网络应用程序,我在页面加载时使用数据库中的项目填充列表框。
I have web application in which i populate listbox using items from database on page load.
我从列表框中选择了所有项目,但它们是按顺序排列的他们居住在那里。
I get the all items from listbox that are selected but they are in order in which they populated.
我希望这些项目符合用户选择的顺序。
I want these items to be in the order that they are selected by user.
我试过 .change(function()
使用jQuery但它只返回第一个选定的项值。
I tried .change(function()
using jQuery but it returns only first selected items value.
我附上我的代码以供参考。我使用 http://harvesthq.github.com/chosen/
I attaching my code for reference. I have used listbox using http://harvesthq.github.com/chosen/
这是我的列表框:
<asp:ListBox ID="dr_menuitem" runat="server" class="chzn-select" SelectionMode="Multiple" style="width:300px;" >
<asp:ListItem></asp:ListItem>
</asp:Listbox>
这是我调用的jQuery:
This is the jQuery I call:
<script type="text/javascript">
$(document).ready(function() {
$('#dr_menuitem').change(function() {
alert($("#dr_menuitem option:selected").val());
});
});
</script>
推荐答案
要按所选顺序获取所选值,请尝试以下操作:
To get the selected value in selected order try this:
$("#dr_menuitem").change(function() {
// $(this).val() will give you an array
// of selected value in order
// you've selected
alert( $(this).val() );
});
DEMO
这篇关于如何从多选列表框中获取所选项目以便选择它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!