简体   繁体   中英

javascript for listbox items

如果用户从一个列表框移动到另一个列表框时,如果列表中已有该项目,请提出如何覆盖列表中的项目。

you can just use jquery insertAfter

    $(document).ready(function(){
    $('.addtoright').on('click',function(e){
        e.preventDefault();
        $('#s option:selected').each(function(){
            if( $('#d option:contains("'+$(this).text()+'")').length > 0){ 
                $(this).remove();
            }else{
                if($('#d option').length > 0){
                   $(this).insertAfter('#d option:last');
                }else{
                    $(this).appendTo('#d');
                }            
            }
        });
    });
    $('.addtoleft').on('click',function(e){
        e.preventDefault();
        $('#d option:selected').each(function(){
            if( $('#s option:contains("'+$(this).text()+'")').length > 0){ 
                $(this).remove();
            }else{
                if($('#s option').length > 0){
               $(this).insertAfter('#s option:last');
                }else{
                    $(this).appendTo('#s');
                }
            }
        });
       });
});

DEMO HERE

Dont forget to add class addtoright to right arrows and addtoleft to left arrows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM