2012-03-22 59 views
1

即時通訊真的對不起,我不知道如何訪問這一個: 我試圖有一個按鈕,單擊時,計算總數量是一個數組,因爲它是動態的,但訪問方法不工作。如何使用javascript訪問我的動態表單中的元素?

這裏是我的添加和刪除的javascript:

function addRow(tableID) { 
    var table = document.getElementById(tableID); 
    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount); 
    var colCount = table.rows[0].cells.length; 

    for(var i=0; i<colCount; i++) { 
     var newcell = row.insertCell(i); 
     newcell.innerHTML = table.rows[0].cells[i].innerHTML; 

     switch(newcell.childNodes[0].type) { 
      case "text": 
       newcell.childNodes[0].value = ""; 
       break; 
      case "checkbox": 
       newcell.childNodes[0].checked = false; 
       break; 
      case "select-one": 
       newcell.childNodes[0].selectedIndex = 0; 
       break; 
     } 
    } 
} 

function deleteRow(tableID) { 
    try { 
     var table = document.getElementById(tableID); 
     var rowCount = table.rows.length; 

     for(var i=0; i<rowCount; i++) { 
      var row = table.rows[i]; 
      var chkbox = row.cells[0].childNodes[0]; 
      if(null != chkbox && true == chkbox.checked) { 
       if(rowCount <= 1) { 
        alert("Cannot delete all the rows."); 
        break; 
       } 
       table.deleteRow(i); 
       rowCount--; 
       i--; 
      } 
     } 
    }catch(e) { 
     alert(e); 
    } 
} 

,然後我有這個動態的項目增加:使用

<tr>        
    <td>Items</td> 
    <td style="text-align:left;"> 
     <INPUT type="button" value="Add New Item" onclick="addRow('dataTable')" /> 
     <INPUT type="button" value="Delete Item" onclick="deleteRow('dataTable')" /> 
     <TABLE id="dataTable" width="350px" > 
      <TR> 
       <TD><INPUT type="checkbox" name="chk[]"/></TD> 
       <TD> 
        <select name="ItemNo[]" id="select" value="ItemNo" > 
         <?php   
          $sql2="select * from jewelry_system.item where NumStored !='0' order by ItemName asc"; 
          $result2 = mysql_query($sql2); 
               while($row2=mysql_fetch_array($result2)){ 
         ?> 
         <option value="<?php echo $row2['ItemNo']?>:<?php echo $row2['SalePrice']?>"> <?php echo $row2['ItemName'];?> [ Php <?php echo $row2['SalePrice'];?> Stocks Left:<?php echo $row2['NumStored'];?> ]</option>               <?php } ?> 
         </select> 
       </TD> 
      </TR> 
     </TABLE >   
    </td>           
</tr> 

,並通過此功能可以單獨我貨號值以及所述銷售Priceof該項目,然後myfunction不會工作的加法是checktotal():

function findexts(f){ 
    f=strtolower(f); 
    exts=split('[/\\:]', f); 
    n=count(exts)-1; 
    exts=exts[n]; 
    return exts; 
} 
function checkTotal(){ 
    var total = 0; 
    var temp = 0; 
    var itemArray = document.trans.ItemNo; 

    for(var i=0;i<itemArray.length;i++){ 
     temp = findexts(itemArray[i].value); 
     total += temp; 
    } 
    alert("Total Payment: "+total); 
} 

有人可以騰出timeplease.I真的需要這個。謝謝你

回答

0
function checkTotal() 
{ 
    var total = 0; 
    var parts = null; 

    // Your select input has an ID of "select", so this will iterate through each option 
    $('#select option').each(function() { 
     // The Item # and Sale Price are separated by a colon (:) 
     parts = $(this).val().split(':'); 

     // Increment the total with the Sale Price of each item 
     total += parseFloat(parts[1]); 
    }); 

    alert("Total Payment: "+total); 
} 
//also The item $ will iterate so try this 
// The Item #,$ and Sale Price are separated by a colon (:) 
     parts = $(this).val().split(':'); 

     // Increment the total with the Sale Price of each item 
     total += parseFloat(parts[1]); 
    }); 

    alert("Total Payment: "+total); 
} 
+0

所以我的訪問方法好嗎? 這個零件: var itemArray = document.trans.ItemNo; ??? sincein formitsnameis產品編號[] – 2012-03-22 16:39:34

+0

我沒有任何使用document.trans的經驗,所以我將這部分留下了。我將用一些jQuery更新我的答案以檢索選擇選項值。 – augustknight 2012-03-22 16:53:45

+0

哎呀,那parseInt應該是一個parseFloat,以防你的銷售價格作爲十進制值。也會更新。 – augustknight 2012-03-22 16:58:15

相關問題