使用JQuery将项目从Dropdownlist添加到ListBox [英] Add Item from Dropdownlist to ListBox using JQuery

查看:60
本文介绍了使用JQuery将项目从Dropdownlist添加到ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JQuery将从下拉列表中选择的任何用户添加到列表框中?并且当我发布页面时,我应该能够从列表框中检索"id,名称" .

How can i add anything user selected from dropdownlist to listbox using JQuery? and when i post the page i should be able to retrieve "id, name" from the listbox.

<asp:DropDownList ID="ddlPerson" DataSourceID="ods_person" 
                DataValueField="Id" DataTextField="Name" runat="server" Width="221px" /><br />

                <asp:ListBox ID="lstPerson" runat="server" Width="245px"  
                Font-Bold="true" ForeColor="Green" SelectionMode="Multiple">
                </asp:ListBox> <br>

推荐答案

要将项目添加到下拉列表中,可以使用以下脚本

To add the items to the dropdown you can use the following script

<script type="text/javascript">
    $(function ()
    {
        $('[id$=items]').change(function (e)
        {
            var option = $('<option/>').html($(e.currentTarget).val());
            $('[id$=listBox]').append(option);
        });
    });
</script>

但是,您会遇到问题.添加到列表框的项目将不会保留在服务器上列表框的项目"中.您需要将这些值添加到客户端上的隐藏字段中,然后在服务器上拆分这些值.

BUT you will run into an issue. Items added to listbox will not be persisted back into the list box's "Items" on the server. You will need to add these values into a hidden field on the client and split up the values on the server.

这篇关于使用JQuery将项目从Dropdownlist添加到ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆