2010-08-11 75 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;