js动态添加一行。其中一个单元格是<select id="Destination'+rowNum+'" name="Destination'+rowNum+' style="width:180;"/>该页面有一个dropdown控件,当该控件的值改变时,将其相关的数据填充到select里.

解决方案 »

  1.   

    只能用JS操作了,但是select中的数据也只能是静态的啊,刷新页面肯定就没有了啊
      

  2.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
        <title></title>    <script language="javascript" type="text/javascript">
            function setSelect() {
                var dropdown = document.getElementById("<%=DropDownList1.ClientID %>");
                var s = document.getElementById("Select1");
                if (dropdown.value == "Microsoft") {
                    s.options.length = 0;
                    s.options.add(new Option("Dotnet", "Dotnet"));
                    s.options.add(new Option("C#", "C#"));
                    s.options.add(new Option("Win7", "Win7"));
                }
                if (dropdown.value == "IBM") {
                    s.options.length = 0;
                    s.options.add(new Option("Eclipse", "Eclipse"));
                    s.options.add(new Option("DB2", "DB2"));
                }
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="return setSelect();">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem>Microsoft</asp:ListItem>
            <asp:ListItem>IBM</asp:ListItem>
        </asp:DropDownList>
        </form>
        <select id="Select1" name="D1">
            <option></option>
        </select>
    </body>
    </html>
      

  3.   

       <script language="javascript" type="text/javascript">
            var rowNum = '<%=ViewState["rowNum"] %>';
            if (rowNum == 0)
                rowNum = 1;
            function CreatTicketPrice() {
                var ticketPriceID = newGuid();
                arrCols = new Array(                  
                          '<select id="Destination'+rowNum+'" name="Destination'+rowNum+' style="width:180;"/>',
                          '<select id="IsEndStation' + rowNum + '" name="IsEndStation' + rowNum + '"><option>是</option><option>                         否</option></select>',
                          '<input type="text"   name="TravelTime' + rowNum + '" class="infoInput" />');
                InsertRow(arrCols);
                rowNum++;
                document.getElementById("hdCount").value = rowNum;
    }
            function newGuid() {
                var guid = "";
                for (var i = 1; i <= 32; i++) {
                    var n = Math.floor(Math.random() * 16.0).toString(16);
                    guid += n;
                    if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                        guid += "-";
                }
                return guid;
            }
            function InsertRow(cols) {
                arrCols = new Array();
                arrCols = cols;
                row1 = tbTicketPriceList.insertRow();
                for (i = 0; i < arrCols.length; i++) {
                    cell1 = row1.insertCell();
                    cell1.style.cssText = "  text-align: left;  padding: 2px; background-color: #FFFFFF;     height: 30px;";
                    cell1.innerHTML = arrCols[i];
                    
                    
                }
            } 
    </scirpt>
    <html>
    <body>
    <form runat ="server">
     <input type="hidden" name="hdCount" id="hdCount" runat="server" />
     <input id="btnAdd" type="button" value="新增票价" onclick="CreatTicketPrice();" />
            <asp:DropDownList ID="ddlLineType" runat="server" AutoPostBack="True">
                                <asp:ListItem>广洲</asp:ListItem>
                                <asp:ListItem>东莞</asp:ListItem>
                            </asp:DropDownList>
    </form>
    </body>
    </html>
    当选择东莞时,把东莞的城区加载到Destination里。
    当新一行,如果dropdownlist默认值是广洲,则把广洲的城区加载到Destination里.
      

  4.   

    看下js的数据绑定,dropdown改变的时候select重新绑定下
      

  5.   

    AutoPostBack="True"自动回发了,js生成的代码会没有的,你采用ajax去读取数据就可以了