var opt = new Option(label, data);
selectObject.add(opt);

解决方案 »

  1.   

    好象这样只是在前台添加option,但是我后台得出的数据怎么和option挂钩呢~请教
      

  2.   

    后台数据写到建立的Option的innerHTML和value咯
      

  3.   

    示例。。我不知道你怎么从数据库取数据丫刚刚另一个贴里面的问题基本是一样的,在那边写了点代码,给你参考
    http://community.csdn.net/Expert/topic/5347/5347571.xml?temp=.1738397
      

  4.   

    恩,已经参考,那是昨天忘了我自己的ID,借同事的ID发的
    但是还是有点不明白,我从数据库取的数据是在后台用dataset来取的,值全在dataset里面,其实问题是出在(for int i =0;i小于多少,怎么能够从dataset里取所有的值的count,意思是后台dataset的数据怎么和前台的option结合,指教,在线等~
      

  5.   

    我觉得那应该在后台去写入option了。思路应该是一样的
    因为我们公司有一个成熟的ajax和webservice中间层,所以已经很久没有操作后台了,建议你去aspnet版问问看:)
      

  6.   

    使用ajax吧,可以实现你的效果
      

  7.   

    ajax效果不是很好,有点延迟~
    仍然感谢cuigod
      

  8.   

    先 DataTable xx=objDataSet.Tables["tableName"]然后
    <%@ Page Language="C#" AutoEventWireup="True" %>
    <%@ import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>shawl.qiu template</title>
    <script runat="server">
     void Page_Load(Object s, EventArgs e)
     {
      DataTable dt=new DataTable();
      dt.Columns.Add(new DataColumn("id", typeof(Int32)));
      dt.Columns.Add(new DataColumn("text", typeof(String)));
      
      DataRow dr;
      for(Int32 i=0; i<5; i++)
      {
       dr=dt.NewRow();
       dr[0]=i;
       dr[1]="item "+i.ToString();
       dt.Rows.Add(dr);
      }
      
      DataView dv=new DataView(dt);
      ddl.DataSource=dv;
      ddl.DataTextField = "id";
      ddl.DataValueField = "text";  ddl.DataBind();
     } // end Page_Load
    </script>
    </head>
    <body>
     <form runat="server">
      <asp:DropDownList id='ddl' runat='server' >
       <asp:ListItem value='1'>item 1</asp:ListItem>
       <asp:ListItem>item 2</asp:ListItem>
       <asp:ListItem>item 3</asp:ListItem>
       <asp:ListItem>item 4</asp:ListItem>
       <asp:ListItem>item 5</asp:ListItem>
      </asp:DropDownList>
     </form>
    </body>
    </html>
      

  9.   

    oooo<%@ Page Language="C#" AutoEventWireup="True" %>
    <%@ import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>shawl.qiu template</title>
    <script runat="server">
     void Page_Load(Object s, EventArgs e)
     {
      DataTable dt=new DataTable();
      dt.Columns.Add(new DataColumn("id", typeof(Int32)));
      dt.Columns.Add(new DataColumn("text", typeof(String)));
      
      DataRow dr;
      for(Int32 i=0; i<5; i++)
      {
       dr=dt.NewRow();
       dr[0]=i;
       dr[1]="item "+i.ToString();
       dt.Rows.Add(dr);
      }
      
      DataView dv=new DataView(dt);
      ddl.DataSource=dv;
      ddl.DataTextField = "id";
      ddl.DataValueField = "text";  ddl.DataBind();
     } // end Page_Load
    </script>
    </head>
    <body>
     <form runat="server">
      <asp:DropDownList id='ddl' runat='server' >
      </asp:DropDownList>
     </form>
    </body>
    </html>
      

  10.   

    纯JS:
    <select name="select" id="select1" name="select1">
    </select>
    <a href="javascript:addST();">加一个</a>
    <script language="javascript">
    function addST()
    {
    var newObj=new Option("show1","111");
    document.getElementById("select1").add(newObj);
    }
    </script>
      

  11.   

    function aClass(aData, aValue, aText) {
        if (aClass.arguments.length < 3)
          alert("传入的参数错误!")
        else {
          this.Data  = aData;
          this.Value = aValue;
          this.Text  = aText;    }
      }  function chgDnCombobox(aSrc, aDes, aClass, aRemain){  
        if (chgDnCombobox.arguments.length < 3)
    alert("传入的参数错误!")
        else if (aSrc.type != "select-one")
          alert("传入的参数错误!")
        else if (aClass == null)
          alert("aClass未定义错误!")
        else {
          if (aRemain==null)
            aRemain = 1;
            
            
          chgDnComboboxItem(aSrc.options[aSrc.selectedIndex].value, aDes, aClass, aRemain);    }
      }  function chgDnComboboxItem(aValue, aDes, aClass, aRemain){
        if (chgDnComboboxItem.arguments.length<3)
          alert("参数传入错误!")
        else if (aClass==null)
          alert("错误!aClass没有定义!")
        else{
          if (aRemain==null)
            aRemain = 1;
            
           
          deleteAllComboboxItem(aDes, aRemain);
            
          for (var i=0; i<aClass.length; i++)
            if (aClass[i].Data==aValue)  /* check aClass[i].Data */
              if (aClass[i].Data!="")
                addComboboxItem(aDes, aClass[i].Text, aClass[i].Value);
        }
      }  function deleteAllComboboxItem(aList, aRemain) {
      
      
        for (var i=aList.options.length; i > aRemain-1; i--)
          aList.options[i] = null;
      }      function addComboboxItem(aList, aText, aValue) {
        var aOption = new Option(aText, aValue);
        eval("aList.options[aList.options.length]=aOption");
      }
      

  12.   

    //数据区,可以在这里添加数据
    //数据区,可以在这里添加数据
    function changeClass(aForm) {

    chgDnCombobox(aForm.ClassId1, aForm.ClassId, f_Set1 , 0);
    }
    function change_area(aForm) {
    chgDnCombobox(aForm.LiveProvince, aForm.LiveCity, f_Set0 , 0);
    }function changeArea(aForm) {

    chgDnCombobox(aForm.HomeProvince, aForm.HomeCity, f_Set0 , 0);
    }
    function changeCommonArea(aForm) {
    chgDnCombobox(aForm.WorldId, aForm.CountryId, f_Set0 , 0);
    }
    function changeCityArea(aForm) {
    chgDnCombobox(aForm.CountryId, aForm.ToClassId, f_Set0 , 0);
    }
    function onchg(aForm) {
    onchg(document.myform);}
    var f_Set0 = new Array(
    new aClass('141','141','请选择北美洲国家'),
    new aClass('141','354','J--加拿大'),
    new aClass('354','355','埃德蒙顿'),
    new aClass('354','356','多伦多'),
    new aClass('354','357','佛雷德里克顿'),
    new aClass('354','358','哈里法克斯'),
    new aClass('354','359','基隆拿'),
    new aClass('354','851','基韦斯德'),
    new aClass('354','360','卡尔加利'),
    new aClass('354','361','魁北克'),
    new aClass('354','362','里贾纳'),
    new aClass('354','363','蒙特利尔'),
    new aClass('354','850','纳奈莫'),
    new aClass('354','852','乔治王子城'),
    new aClass('354','364','萨斯卡通'),
    new aClass('354','365','桑德贝'),
    new aClass('354','366','圣约翰'),
    new aClass('354','367','圣约翰斯'),
    new aClass('354','368','苏圣玛丽'),
    new aClass('354','369','维多利亚'),
    new aClass('354','370','温哥华'),
    new aClass('354','371','温尼伯'),
    new aClass('354','372','温莎'),
    new aClass('354','373','渥太华'),
    new aClass('141','375','M--美国'),
    new aClass('375','376','阿比林'),
    new aClass('375','904','阿波寇尔克'),
    new aClass('375','377','阿尔伯克基'),
    new aClass('375','378','阿尔伯尼'),
    new aClass('375','379','阿马里洛'),
    new aClass('375','380','埃尔帕索'),
    new aClass('375','381','埃文斯维尔'),
    new aClass('375','382','爱伯克奇'),
    new aClass('375','384','爱达华瀑布市'),
    new aClass('375','385','安大略'),
    new aClass('375','386','安吉拉斯港'),
    new aClass('375','387','安克雷奇'),
    new aClass('375','388','敖德萨'),
    new aClass('375','389','奥兰多'),
    new aClass('375','390','奥斯汀'),
    new aClass('375','391','巴尔地摩'),
    new aClass('375','906','巴特'),
    new aClass('375','392','巴吞鲁日'),
    new aClass('375','393','北本德'),
    new aClass('375','394','贝克斯菲尔德'),
    new aClass('375','395','贝灵汉'),
    new aClass('375','396','比灵斯'),
    new aClass('375','397','波卡特洛'),
    new aClass('375','398','波斯顿'),
    new aClass('375','399','波特兰'),
    new aClass('375','400','波特兰(ME)'),
    new aClass('375','905','波西'),
    new aClass('375','401','波兹曼'),
    new aClass('375','402','伯明翰'),
    new aClass('375','403','布法罗'),
    new aClass('375','404','布卢明顿'),
    new aClass('375','405','布特'),
    new aClass('375','406','查塔努加'),
    new aClass('375','407','达拉斯'),
    new aClass('375','408','大急流城'),
    new aClass('375','409','大瀑布市'),
    new aClass('375','410','大学城'),
    new aClass('375','411','代顿'),
    new aClass('375','412','丹佛'),
    new aClass('375','413','得梅因'),
    new aClass('375','414','迪比克'),
    new aClass('375','415','底特律'),
    new aClass('375','416','菲尼克斯(凤凰城)'),
    new aClass('375','417','费城'),
    new aClass('375','907','弗雷斯诺'),
    new aClass('375','418','弗列斯诺'),
    new aClass('375','419','哥伦布'),
    new aClass('375','420','格林贝'),
    new aClass('375','421','格林斯伯勒'),
    new aClass('375','422','格林维尔'),
    new aClass('375','832','关岛'),
    new aClass('375','423','哈里斯堡'),
    new aClass('375','424','哈特福德'),
    new aClass('375','425','海伦娜'),
    new aClass('375','908','汉米尔顿'),
    new aClass('375','426','华盛顿'),
    new aClass('375','427','杰克逊'),
    new aClass('375','428','杰克逊维尔'),
    new aClass('375','429','旧金山'),
    new aClass('375','430','橘子郡'),
    new aClass('375','431','卡拉马祖'),
    new aClass('375','432','卡利斯比'),
    new aClass('375','433','堪萨斯城'),
    new aClass('375','434','科罗拉多斯普林斯'),
    new aClass('375','435','科纳岛'),
    new aClass('375','436','科珀斯克里斯蒂'),
    new aClass('375','437','克拉玛斯瀑布市'),
    new aClass('375','438','克里夫兰'),
    new aClass('375','439','拉伯克'),
    new aClass('375','440','拉克罗斯'),
    new aClass('375','441','拉雷多'),
    new aClass('375','442','拉斯维加斯'),
    new aClass('375','443','莱维斯顿'),
    new aClass('375','444','劳德代尔堡'),
    new aClass('375','445','雷德蒙'),
    new aClass('375','446','里诺'),
    new aClass('375','447','林肯'),
    new aClass('375','448','路易斯维尔'),
    new aClass('375','449','罗利'),
    new aClass('375','450','罗切斯特'),
    new aClass('375','451','洛杉矶'),
    new aClass('375','452','马凯特'),
    new aClass('375','453','迈阿密'),
    new aClass('375','454','迈尔斯堡'),
    new aClass('375','455','麦迪逊'),
    new aClass('375','456','梅德福'),
    new aClass('375','457','蒙特利'),
    new aClass('375','458','孟菲斯'),
    new aClass('375','459','密尔沃基'),
    new aClass('375','460','密苏拉'),
    new aClass('375','461','明尼阿波利斯'),
    new aClass('375','462','摩西莱克'),
    new aClass('375','463','纳斯维尔'),
    new aClass('375','464','纽约'),
    new aClass('375','465','诺福克'),
    new aClass('375','466','诺克斯维尔'),
    new aClass('375','467','帕迪尤卡'),
    new aClass('375','468','帕斯科'),
    new aClass('375','469','彭德尔顿'),
    new aClass('375','470','皮奥里亚'),
    new aClass('375','471','匹兹堡'),
    new aClass('375','472','蒲门'),
    new aClass('375','473','普罗维登斯'),
    new aClass('375','474','萨克拉门托'),
    new aClass('375','475','塞伦'),
    new aClass('375','476','尚佩恩'),
    new aClass('375','477','圣安东尼奥'),
    new aClass('375','478','圣安吉洛'),
    new aClass('375','479','圣芭芭拉'),
    new aClass('375','480','圣迭戈'),
    new aClass('375','481','圣何塞'),
    new aClass('375','482','圣路易斯'),
    new aClass('375','483','圣塔安那'),
    new aClass('375','484','什里夫波特'),
    new aClass('375','485','史密斯堡'),
    new aClass('375','486','士波肯'),
    new aClass('375','487','树城'),
    new aClass('375','488','双子瀑布'),
    new aClass('375','489','斯普林菲尔德'),
    .....
      

  13.   

    如何前后台数据关联。
    我用的是jsp,我就说jsp方法了。
    把数据库中取出来的数据,先写到网页上,代码如下:
    <jsp:useBean id="type" scope="page" class="com.cn.vo.MerchandiseTypeBean"/> <!--一级类型  -->
      <select name="select1" id="select1" onChange="changeSelect(this);">
       <c:forEach var="type1" items="${type.list1}" >
    <option value="${ type1.number}">${type1.name }</option>
    </c:forEach>
      </select>
    代码中,type.list1,是从数据库中取出来的一组ArrayList数据。然后存到网页中定义的Array。
    然后javascript,代码如下:
    var a=document.getElementById("select1");
    var first = new Array(b.options.length);
    for(var i=0;i<b.options.length;i++){
    first[i]= new Array(b.options[i].value,b.options[i].text)
    }现在已经存进去了,剩下的就可以用上面大家说到的方法,自己随便组织数据了
      

  14.   

    function dyn_create_select(op_value,op_name){
        var sel = document.getElementById("select_id");
        if(sel!=null&&typeof(sel)=="object"){
           var oOption = document.createElement("OPTION");
           oOption.value =op_value;
           oOption.innerHTML =op_name;
           sele.appendChild(oOption);
        }
    }
      

  15.   

    function dyn_create_select(op_value,op_name){
        var sel = document.getElementById("select_id");
        if(sel!=null&&typeof(sel)=="object"){
           var oOption = document.createElement("OPTION");
           oOption.value =op_value;
           oOption.innerHTML =op_name;
           sel.appendChild(oOption);
        }
    }
    这样应该可以的吧??快过年了,心思都不在工作上,老敲错字母,晕得不行了
      

  16.   

    <select name="select" id="select1" name="select1">
    </select>
    <a href="javascript:addST('select1','newoption','newvalue');">加一个</a>
    <script language="javascript">
    function addST(obj,op_name,op_value)
    {
    var newObj=new Option(op_name,op_value);
    document.getElementById(obj).add(newObj);
    }
    </script>