我从一个listbox里双击某一项添加到另外一个listbox里,同时这边除去那一项。 在选到项时还好,就是如果没选中项直接双击的话,就会报页面错误说是“发生意外”我的js是这样的:
//雙擊鼠標添加email
        function addDbclick()
        {   
            var addOption=document.createElement("option");
            var index;
            if(document.all("emailaddr").length==0)
            {
                alert("無傚選項");
                return(false);
            }else
            {
                index=document.all("emailaddr").selectedIndex;
            }
            addOption.text=document.all("emailaddr").options(index).text;
            addOption.value=document.all("emailaddr").options(index).value;
            
            document.all("outselect").add(new Option(addOption.text,addOption.value));
            if(!(index<0))
            {
                document.all("emailaddr").options.remove(index);
            }
        }请问怎么解决这个问题? 我对js不熟

解决方案 »

  1.   

    Maybe you should check whether the element be null before use it.
    BTW, I'm not familiar with js either.
      

  2.   

    要先判断listbox中是否有项目或是否选中了项目,否则退出程序
      

  3.   

    function addDbclick()
            {   
                if(document.all("emailaddr").value==null||document.all("emailaddr").value=="")
             {
                 return false;
             }
                var addOption=document.createElement("option");
                var index;
                if(document.all("emailaddr").length==0)
                {
                    alert("無傚選項");
                    return(false);
                }else
                {
                    index=document.all("emailaddr").selectedIndex;
                }
                addOption.text=document.all("emailaddr").options(index).text;
                addOption.value=document.all("emailaddr").options(index).value;
                
                document.all("outselect").add(new Option(addOption.text,addOption.value));
                if(!(index<0))
                {
                    document.all("emailaddr").options.remove(index);
                }
            }