2010-03-04 142 views
1

我有一個按鈕,當按下按鈕時,將添加一個新的HTML行。在這些行中有兩個組合框:cboOffsetGroup和cboOffsetType。根據在cboOffsetGroup中選擇的內容,cboOffsetType的選項將會改變。我在Javascript中使用了一個refreshPage()函數。它在第一行工作正常,但在隨後添加的行中,我無法弄清楚如何調用refreshPage()函數,以便可以爲cboOffsetType填充適當的選項。任何對此的幫助表示讚賞。請注意,我幾乎是這個新手,並認識到這可能不是最好的代碼,但這是我必須與之合作。謝謝!刷新並填充動態添加行

<% 
Dim CN, RS, vIndexOffset, vOffsetGroup 

Set CN = GetDataConnection 

vIndexOffset = Request.Form("txtOffsetIndex") 
%> 

<body> 
<form name="frmExceptionInput" id="frmExceptionInput" method="post"> 
<input type="hidden" name="txtAction" id="txtAction" value=""> 

<table width="50%" id="tblOffsetDetail"> 
<tbody> 
    <tr> 
     <td colspan="2"> 
     <input type="button" class="button" value= "Add New Item" id="btnNewOffsetItem" name="btnNewOffsetItem" onClick="javascript:addNewOffsetItem();"> 
     <input type="hidden" id="txtOffsetIndex" name="txtOffsetIndex" value="1"> 
    </td> 
    </tr> 
    <tr> 
     <td> 
      <p> 
      <select name="cboOffsetGroup1" id="cboOffsetGroup1" onChange="refreshPage();"> 
       <option value="0"></option> 
       <% 
       Set RS = CN.Execute ("spSelectGroup") 
       If Not RS.EOF Then 
        Do While Not RS.EOF 
         %> 
         <option value='<%= RS("offsetGroupID")%>'<%If RS("offsetGroupID") = Request.Form("cboOffsetGroup" & vIndexOffset) Then Response.Write " selected"%>><%= RS("offsetGroup")%></option>  
         <% 
         'Get next record 
         RS.MoveNext 
        Loop 
       End If 
       %> 
      </select> 
      </p>   
     </td> 
     <td> 
      <p> 
      <select name="cboOffsetType1" id="cboOffsetType1"> 
      <% 
      'If the user changes the combo box selected, set vOffsetGroup = the value selected by the user on the page. 
      If Request.Form("txtAction") = "Change Selection" Then 
       vOffsetGroup = Request.Form("cboOffsetGroup" & vIndexOffset) 
      End If 
      %> 
      <option value="0"></option> 
      <% 
      Set RS = CN.Execute ("spSelectType @vOffsetGroup='" & vOffsetGroup & "'") 
      If Not RS.EOF Then 
       Do While Not RS.EOF 
        %> 
        <option value='<%= RS("offsetItemTypeID")%>'<%If Request.Form("cboOffsetType1") = RS("offsetItemTypeID") Then Response.Write "selected"%>><%= RS("offsetItemType")%></option> 
        <% 
        'Get next record 
        RS.MoveNext 
       Loop 
      End If 
      %> 
      </select> 
      </p> 
     </td> 
    </tr> 
</tbody> 
</table> 

</form> 
</body> 

<script language="javascript"> 

function refreshPage() 
{ 
    var refreshPage = document.getElementById("frmExceptionInput"); 

    //If this function is called, the value of txtAction will become "Change Selection." 
    document.getElementById("txtAction").value = "Change Selection"; 

    refreshPage.submit(); 
} 

//Display additional rows. 
function addNewOffsetItem() 
{ 
    var iX = document.getElementById("txtOffsetIndex").value; 
    iX ++; 
    document.getElementById("txtOffsetIndex").value = iX; 

    var tbl = document.getElementById("tblOffsetDetail").getElementsByTagName("TBODY")[0]; 
    var tr = document.createElement("TR"); 
    tbl.appendChild(tr); 

    //cboOffsetGroup1 
    var tdOffsetGroup = document.createElement("TD"); 
    tr.appendChild(tdOffsetGroup); 

    var p = document.createElement("P"); 
    tdOffsetGroup.appendChild(p); 

    var cboOffsetGroup = document.createElement("select"); 
    p.appendChild(cboOffsetGroup); 

    cboOffsetGroup.id = "cboOffsetGroup" + iX; 
    cboOffsetGroup.setAttribute('name','cboOffsetGroup' + iX); 

    var cboOffsetGroup1 = document.getElementById("cboOffsetGroup1"); 
    var i = 0; 

    for (i = 0; i < cboOffsetGroup1.children.length; i++) 
     { 
      var opt = document.createElement("option"); 
      opt.value = cboOffsetGroup1 [i].value; 
      opt.innerText = cboOffsetGroup1 [i].innerText; 
      cboOffsetGroup.appendChild(opt); 
     } 

    //cboOffsetType1 
    var tdOffsetType = document.createElement("TD"); 
    tr.appendChild(tdOffsetType); 

    var p = document.createElement("P"); 
    tdOffsetType.appendChild(p); 

    var cboOffsetType = document.createElement("select"); 
    p.appendChild(cboOffsetType); 

    cboOffsetType.id = "cboOffsetType" + iX; 
    cboOffsetType.setAttribute('name','cboOffsetType' + iX); 

    var cboOffsetType1 = document.getElementById("cboOffsetType1"); 
    var i = 0; 

    for (i = 0; i < cboOffsetType1.children.length; i++) 
     { 
      var opt = document.createElement("option"); 
      opt.value = cboOffsetType1 [i].value; 
      opt.innerText = cboOffsetType1 [i].innerText; 
      cboOffsetType.appendChild(opt); 
     } 
} 

</script> 
</html> 

回答

1

對於這類任務,您最好使用JQuery。

http://api.jquery.com/add/

+0

我可以看看這包括在新項目日後參考,但真的沒有時間投入到它這個項目。我正在尋找我目前擁有的代碼中的解決方案。 – SeanFlynn 2010-03-05 18:42:20

相關問題