重构《实现select multiple左右添加和删除功能》

本文介绍使用 JQuery 实现 Select 多选框的左右添加和删除功能的方法。通过自定义函数 initCandidateList、attachAddButtonEvent 和 attachDeleteButtonEvent 来初始化候选列表、绑定添加和删除按钮事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很久很久以前,我刚听说过JQuery,然后在不知道javaeye还是blogjava上看到一篇《实现select multiple左右添加和删除功能》的文章,写的很黄很暴力,我一紧张就打印下来了,昨天晚上拉肚子上厕所,突然找出打印稿,在厕所上看了一下,发现不错。这个文章的原作者我已经不知道是谁了,但是我要感谢他的奉献精神。

 

为了表示对他的尊敬,我修改了他的代码,为了更通用。代码例子任然沿用原作者的风格。希望大家喜欢。

 

 

源代码附件也有

原文实现select multiple左右添加和删除功能》转载:http://winhonourxzg.iteye.com/blog/342987 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>jquery life</title>

    <script type="text/javascript" src="jquery1.3.2.js"></script>

    <!--下面这个script元素里面包含的代码您再修改一下,可以做为公用的放到您的项目的common.js里面。-->
    <script type="text/javascript">
          /* initCandidateList: 根据参数初始化候选列表里面的元素*/
        function initCandidateList(candidateListId, listLength, preText) {
            for (var i = 1; i <= listLength; i++)
            {
                $("#" + candidateListId).append("<option value='" + i + "'>" + preText + i + "</option>");
            }
        }

        /*attachAddButtonEvent:给add按钮添加事件*/
         function attachAddButtonEvent(addButtonId, candidateListId, selectedListId, msg) {
            $(function() {
                $("#" + addButtonId).click(function() {
                    if ($("#" + candidateListId + " option:selected").length > 0)
                    {
                        $("#" + candidateListId + " option:selected").each(function() {
                            $("#" + selectedListId).append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option");
                            $(this).remove();
                        })
                    }
                    else
                    {
                        alert(msg);
                    }
                })
            })
        }
        /*attachDeleteButtonEvent:给delet按钮添加事件*/
        function attachDeleteButtonEvent(deleteButtonId, candidateListId, selectedListId, msg) {
            $(function() {
                $("#" + deleteButtonId).click(function() {
                    if ($("#" + selectedListId + " option:selected").length > 0)
                    {
                        $("#" + selectedListId + " option:selected").each(function() {
                            $("#" + candidateListId).append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option");
                            $(this).remove();
                        })
                    }
                    else
                    {
                        alert(msg);
                    }
                })
            })
        }
    </script>

   <!--下面这个script元素里面包含的代码只针对这个页面的功能,就放在这个页面好了。-->
    <script type="text/javascript">
        $(document).ready(function() {
            //first multiple select
            initCandidateList('candidate_list', 5, '公开招标小型机采购00');
            attachAddButtonEvent('add', 'candidate_list', "select_list", '请选择要添加的分包!');
            attachDeleteButtonEvent('delete', 'candidate_list', "select_list", "请选择要删除的分包");

            //second multiple select
            initCandidateList('candidate_list2', 6, '非公开招标大型机采购');
            attachAddButtonEvent('add2', 'candidate_list2', "select_list2", '请选择要添加的分包!');
            attachDeleteButtonEvent('delete2', 'candidate_list2', "select_list2", "请选择要删除的分包");
        })
    </script>

</head>
<body>
<table width="95%" cellpadding="0" align="center" class="listshow" border="1" cellspacing="0">
    <tr>
        <td colspan="4" align="center">选择第一组分包</td>
    </tr>
    <tr>
        <td class="black" width="30%" align="center" height="150">
            <select id="candidate_list" multiple="multiple" style="text-align:center;width:300px;height:150px;">

            </select>
        </td>
        <td align="center" width="5%">
            <input type="button" id="add" value="添加>>"/>
            <br/>
            <br/>
            <input type="button" id="delete" value="<<删除"/>
        </td>
        <td class="black" width="30%" align="center">
            <select id="select_list" multiple="multiple" style=" text-align:center;width:300px;height:150px;">

            </select>
        </td>
    </tr>
</table>
<br>
<table width="95%" cellpadding="0" align="center" class="listshow" border="1" cellspacing="0">
    <tr>
        <td colspan="4" align="center">选择第二组分包</td>
    </tr>
    <tr>
        <td class="black" width="30%" align="center" height="150">
            <select id="candidate_list2" multiple="multiple" style="text-align:center;width:300px;height:150px;">

            </select>
        </td>
        <td align="center" width="5%">
            <input type="button" id="add2" value="添加>>"/>
            <br/>
            <br/>
            <input type="button" id="delete2" value="<<删除"/>
        </td>
        <td class="black" width="30%" align="center">
            <select id="select_list2" multiple="multiple" style=" text-align:center;width:300px;height:150px;">

            </select>
        </td>
    </tr>
</table>


</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值