2017-04-03 69 views
1

我寫了一個代碼來獲取用戶選擇的值的詳細信息。該代碼工作正常,並顯示數組。唯一的問題是顯示錯誤的孩子節點。我在哪裏做錯了?迭代表中的多個行的值

function add_checkout_details(){ 
      var total = $("#total").val(); 
      var product_id = new Array(); 
      var qty = new Array(); 
      var price = new Array(); 
      var amount = new Array(); 

      var table = document.getElementById('myTable'); 
      var rowCount = table.rows.length;  
     for(var i=0; i<rowCount; i++) { 

        var row = table.rows[i]; 
        var txtBox0 = row.cells[0].innerHTML; 
        var txtBox1 = row.cells[2].childNodes[0].value; 
        var txtBox2 = row.cells[3].childNodes[0].value; 
        var txtBox3 = row.cells[4].childNodes[0].value; 
        var txtBox4 = row.cells[8].childNodes[0].value; 

        product_id.push(txtBox0); 
        qty.push(txtBox1); 
        price.push(txtBox2); 
        amount.push(txtBox3); 
        } 



    $.post('controller/add_checkout_details.php',{'product_id[]':product_id,'qty[]':qty,'price[]':price,'amount[]':amount,total:total}, function(data){ 

     $('#error').text(data); 
    }); 
    } 
+0

你可以添加你的表在代碼中的樣子嗎? –

回答

0

節點沒有value屬性。請使用nodeValue

row.cells[2].childNodes[0].nodeValue; 

注意,您可以寫爲:

row.cells[2].firstChild.nodeValue; 

參見textContentinnerText,其中有稍微不同的行爲。