添加很简单的
selName.options[selName.options.length] = new Option("v","t")

解决方案 »

  1.   

    可能我没有表述清楚了,我的意思是说select不是一般只能选择吗?我想假如我要添加的项目选择中没有的时刻,我可以象文本框那样的输入文字,然后下次我就又可以从下拉中找到刚财添加的那项目!
      

  2.   

    数据库或者是xml,用一个提交变换一下的,还有可以用xmlhttp去提交一下的
      

  3.   

    <script>
    function aaa(obj) {
    if (obj.value != "") {
    for (i=0; i<kao.options.length; i++) {
    if (kao.options[i].innerText == obj.value) return;
    }
    kao.options[kao.options.length] = new Option(obj.value,obj.value);
    }
    }
    </script>
    <input id="newoption" onblur="aaa(this)" style="width:100px">
    <select id="kao">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    </select>
      

  4.   

    <HTML><HEAD>
    <SCRIPT language=javascript>
    <!--
    function WebConboBox(CollectionName)

      if(typeof(CollectionName)!="string")
      return("在您 new 一个 WebComboBox 的时候请把您将赋予的变量名也传递进来!");
      this.layer = new WebLayer("meizzWCB");
      this.items = new Array();
      this.add   = function(Source, ShowArrow, RLOp, AddOp)
      {
        var e = new WebComboBoxItem(CollectionName, this.items.length, Source, ShowArrow, RLOp, AddOp);
        this.items[this.items.length] = e; return e;
      }
    }
    function WebComboBoxItem(WCBN, WCBI, SourceObject, IsSArrow, IsRLOp, CanAddOp)
    {
      if(typeof(SourceObject)!="object" && SourceObject.tagName!="SELECT")
        return("请把这个 ComboBox 对应的下拉框对象作为参数带进来!");
      if(SourceObject.name=="") return("绑定的下拉框 name 不能为空!");
      this.source   = SourceObject;
      if (this.source.multiple || (this.source.size && this.source.size>1))return false;
      this.sourceOpLen=this.source.options.length; if(this.sourceOpLen==0) return false;
      this.color    = "#000000";
      this.bgColor  = "#FFFFFF";
      this.fontSize = "9pt";
      this.className= "";
      this.fontFamily   = "宋体";
      this.isShowArrow  = IsSArrow;   //显示下拉框的那个下拉箭头
      this.canAddOption = CanAddOp;   //用户的每次新输入都记录到下拉框中
      this.reserveLastInput = IsRLOp; //当此 canAddOption=false 时是否保留用户的最后一次输入
      this.arroWidth= 18;
      this.userInput= "";
      if(this.source.selectedIndex == -1) this.source.selectedIndex = 0;
      this.index    = this.source.selectedIndex;
      this.text     = this.source.options[this.index].text;
      this.value    = this.source.options[this.index].value;
      this.width    = this.source.clientWidth;
      this.height   = this.source.clientHeight;
      var WCBInput  = "<input type=text id='WCBInput_"+ this.source.name +"' style='padding-left: 3;"
      +"width : "+ this.width +"; height : "+ this.height +"; font-size: "+ this.fontSize +"'>";
      this.source.insertAdjacentHTML("afterEnd", WCBInput);
      this.input  = getElementById("WCBInput_"+ this.source.name);
      this.source.style.position = "absolute";
      this.source.style.top  = getAbsTop(this.input);
      this.source.style.left = getAbsLeft(this.input);
      this.divIndex     = -1;
      this.mouse        = false;
      this.iframe       = null;
      this.Layer        = null;
      this.LayerStatus  = false;
      this.WCBItems     = new Array();
      this.iframe = eval(WCBN).layer.iframe;
      this.Layer  = eval(WCBN).layer;
      this.showArrow = function(){this.source.style.display = "";}
      this.hideArrow = function(){if(!this.isShowArrow) this.source.style.display = "none";}
      this.hide      = function(){this.Layer.hide(); this.LayerStatus = false;}
      this.source.attachEvent("onchange", function()
      {
        var CB = eval(WCBN).items[WCBI];
        CB.index  = CB.source.selectedIndex;
        CB.text   = CB.source.options[CB.index].text;
        CB.value  = CB.source.options[CB.index].value;
        CB.input.value = CB.text;   CB.input.select();
      });
      this.match = function()
      {
        var str  = this.userInput = this.input.value;
        if(str.replace(/^\s+|\s+$/, "")==""){this.hide(); return;}
        this.WCBItems.length = 0; var a = this.source.options, s='';
        for(var i=0; i<a.length; i++)
        {
          if(a[i].text!=str && a[i].text.indexOf(str)==0)
          {
            var e = new WebConboBoxDataItem(WCBN, WCBI);
            e.index = i; e.text = a[i].text; e.value = a[i].value;
            this.WCBItems[this.WCBItems.length] = e;
          }
        }
        for(var i=0; i<this.WCBItems.length; i++)
        s += this.WCBItems[i].toString(i, this.color, this.bgColor, this.fontSize, this.fontFamily);
        if(s!="") { this.Layer.write(s); this.LayerStatus = true;
        this.Layer.show(this.input, this.width, 108);} else this.hide();
      }
      this.setTopLeft = function()
      {
        this.source.style.top  = getAbsTop(this.input);
        this.source.style.left = getAbsLeft(this.input);
      }
      this.setParam   = function()
      {
        with(this.source.style)
        {
          if(this.color != "") color = this.color;
          if(this.fontSize!= "") fontSize = this.fontSize;
          if(this.bgColor != "") backgroundColor = this.bgColor;
          if(this.fontFamily!= "") fontFamily = this.fontFamily;
          top = getAbsTop(this.input); left = getAbsLeft(this.input);
          this.input.style.width  = this.width  = this.source.clientWidth;
          this.input.style.height = this.height = this.source.clientHeight;
          clip = "rect(2 "+(this.width-2)+" "+(this.height-2)+" "+(this.width-this.arroWidth)+")";
          if (!this.isShowArrow) display = "none";
        } this.input.value = this.text;
        with(this.input.style)
        {
          if(this.color != "") color = this.color;
          if(this.fontSize!= "") fontSize = this.fontSize;
          if(this.bgColor != "") backgroundColor = this.bgColor;
          if(this.fontFamily!= "") fontFamily = this.fontFamily;
        } if(this.className != "") this.input.className = this.className;
      }
      this.input.onfocus     =       function(){eval(WCBN +".items["+ WCBI +"]").showArrow();}
      this.input.onmouseover =       function(){eval(WCBN +".items["+ WCBI +"]").showArrow();}
      this.source.onblur     =       function(){eval(WCBN +".items["+ WCBI +"]").hideArrow();}
      window.attachEvent("onscroll", function(){eval(WCBN +".items["+ WCBI +"]").setTopLeft();});
      window.attachEvent("onresize", function(){eval(WCBN +".items["+ WCBI +"]").setTopLeft();});
      window.attachEvent("onload",   function()
      {eval(WCBN +".items["+ WCBI +"]").setParam(); eval(WCBN +".items["+ WCBI +"]").setTopLeft();});
      this.input.onmouseout  = function(){var CB = eval(WCBN +".items["+ WCBI +"]");
      if(document.activeElement != this && window.event.toElement!=CB.source) CB.hideArrow();}
      //对匹配后的数据展现层的控制
      this.mouseover= function(e)
      {
        var a = this.Layer.layer.document.getElementsByTagName("div");
        if(this.divIndex!=-1){with(a[this.divIndex].style)
          {color=this.color; backgroundColor=this.bgColor;}} this.mouse=true;
        for(var i=0; i<a.length; i++) if(a[i]==e){this.divIndex = i; break;}
        with(e.style){ color="highlighttext"; background="highlight"; }
      }
      this.mouseout = function(e)
      {
        this.mouse=false; this.divIndex = -1;
        with(e.style){ color=this.color; backgroundColor=this.bgColor;}
      }
      

  5.   

    //一页没完,接上面  
    this.arrow    = function(n)
      {
        if(!this.LayerStatus) {this.divIndex = -1; return;}
        if(n==13)
        {
          if(this.mouse)
          {
            this.text = this.input.value = this.WCBItems[this.divIndex].text;
            this.value= this.WCBItems[this.divIndex].value; this.mouse = false;
            this.index= this.source.selectedIndex = this.WCBItems[this.divIndex].index;
            this.hide(); window.event.keyCode = 0; window.event.returnValue = false;
          } return;
        }
        var a = this.Layer.layer.document.getElementsByTagName("div");
        if(this.divIndex!=-1){ this.mouse=false; with(a[this.divIndex].style)
        {color=this.color; backgroundColor=this.bgColor;}}
        if(n==40)
        {
          if(this.divIndex<a.length-1){this.mouse=true; this.divIndex++;
          this.input.value = this.WCBItems[this.divIndex].text;
          with(a[this.divIndex].style){color="highlighttext"; background="highlight";}}
          else {this.mouse=false; this.divIndex=-1; this.input.value = this.userInput;}
        }
        else if(n==38)
        {
          if(this.divIndex!=0){if(this.divIndex==-1) this.divIndex=a.length;
          this.mouse=true; this.divIndex--;
          this.input.value = this.WCBItems[this.divIndex].text;
          with(a[this.divIndex].style){color="highlighttext"; background="highlight";}}
          else {this.mouse=false; this.divIndex=-1; this.input.value = this.userInput;}
        }
    if(this.divIndex > -1)
        {
          var bodyClientHeight  = this.Layer.layer.document.body.clientHeight;
          var bodyScrollTop     = this.Layer.layer.document.body.scrollTop;
          var itemTop           = getAbsTop(a[this.divIndex]) + 16;
          if (itemTop < bodyClientHeight) this.Layer.layer.document.body.scrollTop = 0;
          if (itemTop > bodyClientHeight)
            this.Layer.layer.document.body.scrollTop = itemTop - bodyClientHeight;
        }
      }
      this.input.onkeydown = function()
      {
        var CB = eval(WCBN).items[WCBI];
        switch(window.event.keyCode)   //非IE浏览器, 这里可能会出现小问题
        {
          case 40 : CB.arrow(40); break;
          case 38 : CB.arrow(38); break;
          case 13 : CB.arrow(13); break;
          default : setTimeout(WCBN +".items["+ WCBI +"].match()"); break;
        }
      }
      this.click    = function(n)
      {
        this.text   = this.input.value =this.WCBItems[n].text;
        this.index  = this.source.selectedIndex = this.WCBItems[n].index;
        this.value  = this.WCBItems[n].value; this.hide();
      }
      this.input.onblur = function()
      {
        var CB = eval(WCBN).items[WCBI], A = CB.source.options;
        if(!CB.mouse){ CB.hide(); CB.hideArrow();}
        for(var i=0; i<A.length; i++) if(A[i].text==this.text) return;
        if (CB.canAddOption)
        {
          CB.text = CB.value = CB.userInput;
          A.add(new Option(CB.text, CB.value, true, true));
          CB.source.selectedIndex = CB.index = A.length -1;
        }
        else
        {
          if (CB.reserveLastInput)
          {
            CB.text = CB.value = CB.userInput;
            if(A.length == CB.sourceOpLen) A.add(new Option(CB.text, CB.value, true, true));
            else { A[CB.sourceOpLen].text = CB.text; A[CB.sourceOpLen].value = CB.value; }
            CB.source.selectedIndex = CB.index  = CB.sourceOpLen; return;
          }
          CB.index = CB.source.selectedIndex;
          CB.value = A[CB.index].value;
          CB.text  = CB.input.value = A[CB.index].text;
        }
      }
    }
      

  6.   

    //一页没完,接上面function WebConboBoxDataItem(WCBN, WCBI)
    {
      this.value = this.text = ""; this.index = -1;
      this.toString = function(n, color, bgColor, fontSize, fontFamily)
      {
        return("<div style='color: "+ color +"; background-color: "+ bgColor
          +"; font-size: "+ fontSize +"; font-family: "+ fontFamily +"' "
          +"onclick    ='parent."+ WCBN +".items["+ WCBI +"].click("+ n +")' "
          +"onmouseover='parent."+ WCBN +".items["+ WCBI +"].mouseover(this)' "
          +"onmouseout ='parent."+ WCBN +".items["+ WCBI +"].mouseout(this)' "
          +"nowrap>"+ this.text +"</div>");
      }
    }
    function WebLayer(layerName)
    {
      if(typeof(layerName) != "string") return("The layerName is wrong!");
      this.layer  = this.iframe = null;
      this.IE     = window.navigator.appName == "Microsoft Internet Explorer";
      var  Ver    = window.navigator.appVersion;
      this.popup  = this.IE && parseFloat(Ver.substr(Ver.indexOf("MSIE")+5))>=5.5;
      if (this.popup)
      {
        this.layer= window.createPopup();
        with(this.layer.document.body.style)
        {
          border    = "1px solid #000000";
          overflow  = "auto";
          overflowX = "hidden";
          margin    = "4 0 4 0";
        }
      }
      else
      {
        document.write("<iframe name='WebLayer_"+ layerName +"' id='WebLayer_"+ layerName +"' scrolling=yes"
          +" frameborder=0 border=0 style='position: absolute; display: none; z-index: 10;'></iframe>");
        this.iframe = getElementById("WebLayer_"+ layerName);
        this.layer  = window.frames["WebLayer_"+ layerName];
        this.layer.document.write("<html><head>"
          +" <meta http-equiv='Content-Type' content='text/html; charset=gb2312'></head><body "
          +" style='border: 1px solid #000000; overflow:auto; overflow-x:hidden; margin: 4 0 4 0' "
          +" onselectstart='return false' oncontextmenu='return false'></body></html>");
        this.layer.document.close();
      }
      this.style  = this.layer.document.body.style;
      this.hide   = function()
      {
        if (this.popup){this.layer.hide();}
        else this.iframe.style.display = "none";
      }
      this.write  = function(str)
      {
        if(this.popup) this.layer.document.body.innerHTML =
          "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>"+ str;
        else this.layer.document.body.innerHTML = str;
      }
      this.show   = function(e, width, height)
      {
        if(typeof(e)!="object") return("Layer need a head object!");
        if( !(width && height)) return("Width or Height is wrong!");
        var oh = e.offsetHeight;
        var t  = getAbsTop(e) + oh;
        var l  = getAbsLeft(e);
        var dt = document.body.scrollTop;
        var dl = document.body.scrollLeft;
        var w  = width;
        var h  = height;
        if(this.popup)
        {
          var wt = window.screenTop;
          var wl = window.screenLeft;
          var sw = window.screen.width;
          var sh = window.screen.height;
          this.layer.show(0, 0, 0, 0);
          var ldbs= this.layer.document.body.scrollHeight + 2;
          var h = ldbs > h ? h : ldbs;
          var left = (sw-l-wl+dl > w) ? l-dl+wl+2 : sw-w;
          var top  = (sh-t-wt+dt > h) ? t-dt+wt+2 : (t-dt-oh+wt < h) ? t-dt+wt+2 : t-h-dt-oh+wt;
          this.layer.show(left, top, w, h);
        }
        else
        {
          var dw  = document.body.clientWidth;
          var dh  = document.body.clientHeight;
          var ldbs= this.layer.document.body.scrollHeight + 2;
          with(this.iframe.style)
          {
            display = "";
            width   = w;
            height  = ldbs > h ? h : ldbs;
            top     = (dh+dt-t > h) ? t : (t-dt-oh<h) ? t : t-dt-oh;
            left    = (dw+dl-l > w) ? l : dw+dl-w;
          }
        }
      }
    }
    function getAbsLeft(e){var l=e.offsetLeft; while(e=e.offsetParent) l += e.offsetLeft; return l;}
    function getAbsTop(e){ var t=e.offsetTop;  while(e=e.offsetParent) t += e.offsetTop;  return t;}
    function getElementById(id)
    {
      if (arguments.length == 0) return null;
      /*@cc_on @*/ /*@if (@_jscript_version>=3) return document.all(id); @end @*/
      try {return document.getElementById(id);} catch(e){ return eval(id);}
    }
    var WCB = new WebConboBox("WCB"); //所有的 Combo Box 公用一个数据展示层
    // -->
    </SCRIPT><SCRIPT language=JavaScript src="WebComboBox.js"></SCRIPT><STYLE>input{font-size: 9pt}</STYLE><FORM name=mm method=post>
    <META content="MSHTML 6.00.2800.1505" name=GENERATOR></HEAD>
    <BODY><SELECT class=select id=ddMoneyType name=ddMoneyType> <OPTION value="" selected>所有类型</OPTION> <OPTION value=0>0人民币</OPTION> <OPTION value=1>1美 元</OPTION> <OPTION value=2>2港 币</OPTION></SELECT><BR><SELECT class=select id=ddMoneyType name=ddMoneyType2> <OPTION value="" selected>所有类型</OPTION> <OPTION value=0>0人民币</OPTION> <OPTION value=1>1美 元</OPTION> <OPTION value=2>2港 币</OPTION></SELECT>
    <SCRIPT language=JavaScript><!--
    var A = document.forms["mm"].elements
    for (var i=0; i<A.length; i++)
    {
      if(A[i].tagName == "SELECT")
      {
        var a = WCB.add(A[i], true);
      }
    }
    //--></SCRIPT>
     </BODY></FORM></HTML>
      

  7.   

    如果确实不行联系我。33540135用一个dhtml控件
      

  8.   

    >>yousite1(国雾)  2005-07-19 9:04:00
    如果确实不行联系我。33540135用一个dhtml控件
    能分享下吗  hcqenjoy#126.com
      

  9.   

    那,我拼凑了一个
    --------------<script>
    function fnc(){
    re_name.value=r0.options[r0.selectedIndex].text
    }function fncAdd(){
    var txt=document.all.re_name.value;
    if (txt.length>0){
    var op = document.createElement("OPTION");
    r0.options.add(op);
    op.innerHTML = txt;
    }
    }
    </script><input type=text id=re_name style="width:100px;height:20px;font-size:10pt;"><span
         style="width:2px;border:0px;">
        <select id="r0" style="margin-left:-100px;width:118px;" onChange="fnc();">
            
            <option>111111</option>
            <option>222222</option>
            <option>333333</option>    </select> 
    </span> 
    <span id=o_o><input type="button" value="add a new" onclick="fncAdd();"></span>