一个下列列表,一个文本框,一个按钮
点按钮随机添加一个数字到下拉列表中,选中状态
如果重复则不添加,
在下拉列表中选择某项,在文本框中显示出来
有点内似聊天室,由于初学javascript希望大家帮忙

解决方案 »

  1.   

    以下为可以接受输入的select
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head> <body bgcolor="#FFFFFF" text="#000000">
    <select  style="position:absolute; left: 0px;  top: 0px; width: 120px; height: 18px; clip: rect(0 120 18 100)" id="select" onchange="ReSetSelect()">
      <option value="aaaaaaaa">aaaaaaaa</option>
      <option value="bbbbbbbb">bbbbbbbb</option>
      <option value="cccccccc">cccccccc</option>
    </select>
    <input type="text" style="position:absolute; left: 0px; top: 0px; width: 100px; height: 18px" id="textfield"/>
    <script type='text/javascript'>
    function isContain()
    {
      for(var i=0;i<select.options.length;i++)
      {
        if(select.options[i].text==textfield.value)
          return true;
      }
      return false;
    }
    function ReSetSelect()
    {
      if(textfield.value!=""&&!isContain())
      {
       var o=new Option(textfield.value,textfield.value);
       select.options.add(new Option(textfield.value,textfield.value));
       textfield.value="";
     }
     textfield.value=select.value;
     textfield.select();
    }
    </script>
    </body>
    </html>
      

  2.   

    showbo(iloveC#) 都提供了方法和DEMO给你了,至于你要的啥效果应该结合showbo(iloveC#) 提供给你的方法加上自己的思路写出来。
      

  3.   

    <input type="button" id="btn" onclick="addSel();"/>
    <select  id="select" onchange="resetSelect(this);"></select>
    <input type="text" id="textfield"/>function addSel(){
    var strRnd = parseInt(Math.random() * 10).toString();
    var blnFlag = true; if (document.forms[0].sel.options.length != 0) {
    for (var i = 0; i < document.forms[0].sel.options.length; i++) {
    if (document.forms[0].sel.options[i].value == strRnd) {
    blnFlag = false;
    }
    }
    } if (blnFlag == true) {
    document.forms[0].sel.options[document.forms[0].sel.options.length] = new Option(strRnd, strRnd);
    document.forms[0].sel.options[document.forms[0].sel.options.length - 1].selected = true;
    }
    }
    function resetSelect(sel) {
    document.forms[0].txt.value = sel.options[sel.selectedIndex].value;
    }