思想:建立一个数组,往数组中存元素的时候遍历所有数组元素判断是否有重复元素,没有就不存

解决方案 »

  1.   

    <script language="JavaScript">
    <!--
    function add(src){
    if(event.keyCode!=13){
    document.getElementById("divMsg").innerText="";
    return false;
    }
    var dlt=document.getElementById("dltShow");
    for(var i=0;i<dlt.options.length;i++)
    if(dlt.options[i].value.toLowerCase()==src.value.toLowerCase()){
    document.getElementById("divMsg").innerText="该项已经存在!";
    return false;
    }
    var opt = document.createElement("option");
    dlt.add(opt);
    opt.value=src.value;
    opt.text=src.value;
    dlt.selectedIndex=dlt.options.length-1;
    src.value="";
    }
    //-->
    </script>
    <body onload="document.getElementById('txtInput').focus()">
    <input id="txtInput" type="text" style="width:100px" onkeypress="add(this)">
    <select id="dltShow" style="width:100px"></select>
    <div style="width:600; height:20;" id="divMsg"></div>
    </body>