gpt4 book ai didi

jquery - 如何从多选列表框中获取选定的项目以使其被选中?

转载 作者:行者123 更新时间:2023-12-01 01:54:19 25 4
gpt4 key购买 nike

我有一个网络应用程序,我在页面加载时使用数据库中的项目填充列表框。

我从列表框中获取了所有选定的项目,但它们是按照填充的顺序排列的。

我希望这些项目按照用户选择的顺序排列。

我尝试使用 jQuery .change(function() 但它只返回第一个选定的项目值。

我附上我的代码以供引用。我使用 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:

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

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

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

</script>

最佳答案

要按所选顺序获取所选值,请尝试以下操作:

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

// $(this).val() will give you an array
// of selected value in order
// you've selected

alert( $(this).val() );
});

<强> DEMO

关于jquery - 如何从多选列表框中获取选定的项目以使其被选中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11263823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com