不知道什么原因,在IE上运行出错,在firefox运行正确,代码如下
<!---###html###->
<select id="select1" multiple="multiple" size=20>
<option>可以选择的水果</option>
<option>--------------</option>
<option value="大香蕉">香蕉</option>
<option value="大苹果">苹果</option>
<option value="大橘子">橘子</option>
<option value="大火龙果">火龙果</option>
</select>
<input type="button" id="add" value=">>" onclick="javascript:addSelect()"/>
<input type="button" id="del" value="<<" onclick="javascript:delSelect()"/>
<select id="select2" multiple="multiple" size=20>
<option>已经选择的水果</option>
<option>--------------</option>
</select><!--###js###-->
function addSelect()
{
var mysel1=document.getElementById("select1");
var mysel2=document.getElementById("select2");
var index=mysel1.selectedIndex;
var len=mysel2.options.length;
if(index>1)
{
mysel2.options[len]=mysel1.options[index];  //出错,对象为空,或者不存在此对象
//mysel2.options[len].text=mysel1.options[index].value; //出错,没反应
}
};

解决方案 »

  1.   

    mysel2中只有两个OPTION啊。当你的len值等于2 的时候。去哪里找一个选项给mysel1的值赋给他。这里应该给mysel2新建一个option吧。再把值添加进这个新添加的option
      

  2.   


    var option = new createElement('Option');
    option.value = mysel1.options[index].value;
    mysel2.appChild(option);
    不知道对不对
      

  3.   

    mysel2.add(new Option(mysel1.options[index].text,mysel1.options[index].value));
      

  4.   


    var option = document.createElement('Option');
    option.value = mysel1.options[index].value;
    mysel2.appChild(option);
      

  5.   

    function addSelect()
    {
    var mysel1=document.getElementById("select1");
    var mysel2=document.getElementById("select2");
            var index=mysel1.selectedIndex;
    if(index>1)
    {
               mysel2.appendChild(mysel1.options[index]);
    }
    }
      

  6.   


    function addSelect(){
    var mysel1=document.getElementById("select1");
    var mysel2=document.getElementById("select2");
    var index=mysel1.selectedIndex;
    var len=mysel2.options.length;
    //alert(len)
    if(index>1){
    var val=mysel1.options[index].value;
    mysel2.options[len]=new Option(val,val);
    }
    };
      

  7.   

    http://wyf.javaeye.com/blog/473292   这个不错
      

  8.   

    哥们你这最简单
    不过我在IE上试了试,正常
    在firefox上没反应,郁闷。。