2016-08-31 45 views
0

我需要从表中的每个TR和“推动”他们得到TD输入值到一个数组如何迭代表格的td inputs/tr?

这是我的方法:

function preop() 
{ 
    var ped = []; 
    var table = document.getElementById("latabla"); 

    for (var i = 0, row; row = table.rows[i]; i++) 
    { 
    var articulo=""; 
    var desc = ""; 
    var cant = 0; 
    var precio = ""; 
    var moneda = ""; 
    var PrecV = ""; 
    var prov = 0; 
    //iterate through rows 
    //rows would be accessed using the "row" variable assigned in the for loop 
    for (var j = 0, col; col = row.cells[j]; j++) 
    { 
     //iterate through columns 
     //columns would be accessed using the "col" variable assigned in the for loop 
     if(col[i].name.indexOf('NParte') == 0) 
     { 
     articulo = col[i].value; 
     } 
     if(col[i].name.indexOf('NDesc') == 0) 
     { 
     desc = col[i].value; 
     } 
     if(col[i].name.indexOf('NCant') == 0) 
     { 
     cant = col[i].value; 
     } 
     if(col[i].name.indexOf('Npre') == 0) 
     { 
     precio = col[i].value; 
     } 
     if(col[i].name.indexOf('Nmon') == 0) 
     { 
     moneda = col[i].value; 
     } 
     if(col[i].name.indexOf('NPV') == 0) 
     { 
     PrecV = col[i].value; 
     } 
     if(col[i].name.indexOf('NProv') == 0) 
     { 
     prov = col[i].value; 
     } 
    }//termina td 
    ped.push({"Articulo":articulo,"Descripcion":desc,"Cantidad":cant,"Precio":precio,"Moneda":moneda,"Precio_Venta":PrecV,"Proveedor":prov}); 
    }//termina tr 
    console.log(ped); 
} 

我的错误是我没有告诉代码阅读TD

Uncaught TypeError: Cannot read property 'name' of undefined 

的输入值,我想过使用:

var inputs = document.getElementsByTagName("input"); 

但我不知道如何在col循环中使用它

任何建议或者如果你有更好的方法来实现这一点,将不胜感激。

编辑:

<table id="latabla" class='table table-striped table-hover'> 
    <thead> 
    <th>Numero de Parte</th> 
    <th>Descripcion</th> 
    <th>Cantidad</th> 
    <th>Precio</th> 
    <th>Moneda</th> 
    <th>Precio Venta</th> 
    <th>Proveedor</th> 
    </thead> 
    <tr> 
    <td><input type="text" value="" name="NParte" id="NParte"></td> 
    <td><input type="text" value="" name="NDesc" id="NDesc"></td> 
    <td><input type="number" id="NCant" name="NCant" onkeypress='return isNumberKey(event)' min="1" value="0"></td> 
    <td><input type="number" value="0.00" name="Npre" id="NPre" onkeypress='return isNumberKey(event)' min="1"></td> 
    <td><select id="NMon" name="Nmon"><option value="P">MXN</option><option value="D">USD</option></select></td> 
    <td><input type="number" value="0.00" name="NPV" id="NPV" onkeypress='return isNumberKey(event)'></td> 
    <td><input type="text" value="" id="NProv" name="Nprov" onkeypress='return isNumberKey(event)'></td> 
    <td><input type="button" value="-" name="elmenos" id="elmenos" onclick="deleteRow(this)"/></td> 
    </tr> 
</table> 
+2

能否请您发表您的HTML代码呢? –

+0

已加入HTML代码 – Rhopercy

+0

'col'已经'td'。实际上你需要'var input = col.querySelector('input,select');'然后if(input.name ...'。或者甚至更好'row'querySelectorAll('input,select ');' –

回答

0

你可以使用与列标题沿map/reduce动态地建立这个对象:

// get column headers to use as object keys 
var keys = Array.from(document.querySelectorAll("#latabla thead th")).map(function(cell) { 
    return cell.textContent; 
}); 

// get all input rows 
var rows = Array.from(document.querySelectorAll('#latabla tbody tr')); 
// convert each table row form to an object containing input values 
var data = rows.map(function(row) { 
    return Array.from(row.cells).reduce(function(obj, cell, index) { 
     // get the value of input field inside of cell 
     obj[keys[index]] = cell.querySelector('input,select').value; 
     return obj; 
    }, {}) 
}); 

JS Fiddle Example

注意:这会要求你添加一个<tbody>在你的表身行(S)

+0

出于某种原因,只处理第一个td,并在搜索'Descripcion'时抛出错误 – Rhopercy