阿里妈妈,帮你实现网络赚钱梦,流量变成现金!
10-09
04

JS操作Listbox

给力技术 - 给您提供最新最全的Web资源       收藏几个JS代码,Listbox(列表框)的全选,添加删除,上移下移的JS函数
1.这个是全选的函数
    function listbox_selectall(listID, isSelect) {
        var listbox = document.getElementById(listID);
        for(var count=0; count < listbox.options.length; count++) {
            listbox.options[count].selected = isSelect;
    }
}

使用方法:
listbox_selectall('list', true); //全选
listbox_selectall('list', false); //取消全选

2.上移下移的函数
function listbox_move(listID, direction) {
    var listbox = document.getElementById(listID);
    var selIndex = listbox.selectedIndex;

    if(-1 == selIndex) {
        alert("Please select an option to move.");
        return;
    }

    var increment = -1;
    if(direction == 'up')
        increment = -1;
    else
        increment = 1;

    if((selIndex + increment) < 0 ||
        (selIndex + increment) > (listbox.options.length-1)) {
        return;
    }

    var selValue = listbox.options[selIndex].value;
    var selText = listbox.options[selIndex].text;
    listbox.options[selIndex].value = listbox.options[selIndex + increment].value
    listbox.options[selIndex].text = listbox.options[selIndex + increment].text

    listbox.options[selIndex + increment].value = selValue;
    listbox.options[selIndex + increment].text = selText;

    listbox.selectedIndex = selIndex + increment;
}

使用方法,只需要传递Listbox的ID和移动的方向(up或down):
listbox_move('list', 'up'); //将所选项上移
listbox_move('ist', 'down'); //将所选项下移

3.将Listbox选项移到另一个Listbox中
function listbox_moveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);

    for(var count=0; count < src.options.length; count++) {
        if(src.options[count].selected == true) {
                var option = src.options[count];

                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                         dest.add(newOption, null); //标准
                         src.remove(count, null);
                 }catch(error) {
                         dest.add(newOption); // 针对IE的情况
                         src.remove(count);
                 }
                count--;
        }
    }
}

使用方法,传递两个参数,源Listbox的ID和目的Listbox的ID:
listbox_moveacross('srclist', 'destlist');

[本日志由 老狼 于 2010-09-04 11:07 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
文章标签: JS 代码
网摘收录:
相关日志:
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 支持Gravatar头像.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.