How do I get the selected items from a multi-select listbox in order that they were selected?

怎甘沉沦 提交于 2019-12-23 17:30:25

问题


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.

I tried .change(function() using jQuery but it returns only first selected items value.

I attaching my code for reference. I have used listbox using http://harvesthq.github.com/chosen/

This is my listbox:

   <asp:ListBox ID="dr_menuitem" runat="server" class="chzn-select" SelectionMode="Multiple" style="width:300px;" >
                    <asp:ListItem></asp:ListItem>
               </asp:Listbox>

This is the jQuery I call:

<script type="text/javascript">
           $(document).ready(function() {

               $('#dr_menuitem').change(function() {

                     alert($("#dr_menuitem option:selected").val());
                   });
           });

    </script>

回答1:


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




回答2:


Try this jquery,

$(document).ready(function () {
   $("#listbox").multiselect();
});


来源:https://stackoverflow.com/questions/11263823/how-do-i-get-the-selected-items-from-a-multi-select-listbox-in-order-that-they-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!