2010-08-11 52 views
0

這個問題出現在solving my last question之後,我想從隱藏窗體中獲取一些值,但是當我嘗試僅檢索到空字符串時,我只考慮使用數組來存儲引入的信息,但是我想知道是否有可能僅僅在之後檢索它,以及如何檢索它。如何獲取隱藏或自動生成的內容的值?

此外,還有是與一些JavaScript動態生成的表:

function createTable(){ 
     if (document.getElementById("invoiceFormat").rowNumber.value != ""){ 
      rows = document.getElementById("invoiceFormat").rowNumber.value; 
     } 

     var contents = "<table id='mt'><tr>"; 

     if (document.getElementById("invoiceFormat").cb1[0].checked){ 
      contents = contents + "<td class='htd'>Quantity</td>"; 
     }if (document.getElementById("invoiceFormat").cb1[1].checked){ 
      contents = contents + "<td class='htd'>Description</td>"; 
     }if (document.getElementById("invoiceFormat").cb1[2].checked){ 
      contents = contents + "<td class='htd'>Unitary Price</td>"; 
     }if (document.getElementById("invoiceFormat").cb1[3].checked){ 
      contents = contents + "<td class='htd'>Subtotal</td>"; 
     } 

     for (i=4; i<=k; i++){ 
      if (document.getElementById("invoiceFormat").cb1[i].checked){ 
       contents = contents + "<td>" + document.getElementById("invoiceFormat").cb1[i].value + "</td>"; 
      } 
     } 


     contents = contents + "</tr>"; 

     for (j=1; j<=rows; j++){ 
      contents = contents + "<tr>"; 
      for (l=0; l<=k; l++){ 
       if (document.getElementById("invoiceFormat").cb1[l].checked){ 
       hotfix = l +1; 
       contents = contents + "<td> <input id='cell" + j + "_" + hotfix + "' name='cell' type='text' size='15' /> </td>"; 
       } 
      } 
      contents = contents + "</tr>"; 
     } 

     contents = contents + "</table>"; 
     var createdTable = document.getElementById("mainTable"); 
     createdTable.innerHTML = contents; 

    } 

它的創建後,我試圖訪問它,但沒有運氣到目前爲止,我不能得到什麼用戶在創建的輸入字段中輸入。我怎樣才能做到這一點?

我使用的是JavaScript的原料使用jQuery所以帶或不帶庫的答案,歡迎:)

回答

0

的document.getElementById(「invoiceFormat」)。CB 1 [3] .checked 首先我不知道的什麼「.cb1 [3]」在這裏的意思,所以我會忽略它,並會告訴你我將如何解決這個問題:(我認爲「invoiceFormat」是你的表單的ID) 1)在你的表單中設置name屬性你有每個領域。這樣你可以達到他們像document.getElementById("invoiceFormat").fieldName.value

如果你會使用這種方法確保把你的表單放在一個局部變量。這將是快了很多

var form = document.getElementById("invoiceFormat"); 
form.fieldName.value; 

2)給你一個唯一的ID,只是在字段中使用的getElementById不直接對錶單中的每個領域。 如果這種方法比較好,我不會提起訴訟,但我一直在使用第二種方法。我想我已經習慣了。

3)還有一種方法,但它可能是一種矯枉過正。當你創建你的表單域時,你可以把它們放在一個對象中(不是數值,而是元素本身),甚至將它隱藏在closure中。這樣,你可以打電話像

formElements.formFieldOne.value;