﻿// JScript 文件
/*
    操作select项
*/

    /*添加*/
    function addSelectItem(sourceId,targetId,maxNum)
    {
　　    if($('#' + sourceId + ' option:selected').length>0)
　　    {
　　       var targetNum = $("#" + targetId + ' option').length; 
　　       var selectNum = $('#' + sourceId + ' option:selected').length
　　       if(maxNum>0)
　　       {
　　           if(targetNum + selectNum > parseInt(maxNum))
　　           {
　　               alert('最多可添加' + maxNum.toString() + '项');
　　               return ; 
　　           }
　　       }
　　       
　　       var $target = $('#' + targetId);
　　       var count = $target.get(0).options.length;
            $('#' + sourceId + ' option:selected').each(function(){
                //判断是否存在此值
                var isExist = false; 
                for(var i=0;i<count;i++)   
                {   
                    if($target.get(0).options[i].value == $(this).val())   
                    {   
                        alert('已存在此项');  
                        isExist = true; 
                        break;   
                    }   
                } 
                if(!isExist)
                {
                    $("#" + targetId).append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option");
                    $(this).remove();　
                }
            })
　　    }
　　    else
　　    {
　　　　　　    alert("请选择要添加的项！");
　　    }
    }
    
    /*删除*/
    function removeSelectItem(sourceId,targetId)
    {
        if($('#' + targetId + ' option:selected').length>0)
        {
            $('#' + targetId + ' option:selected').each(function(){
                $('#' + sourceId).append('<option value="'+$(this).val()+'">'+$(this).text()+'</option');
                $(this).remove();　
            })
        }
        else
        {
            alert("请选择要删除的项！");
        }
    }
