JS获取table input输入框的值

var lineHtml = "";
lineHtml += ' <tr>';
lineHtml += ' <td id="RowNo">' + gIndex + '</td>';
lineHtml += ' <td ><input type="checkbox" name="chk" id="chk' + gIndex + '" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" value="1" /></td>';
lineHtml += ' <td style="display:none" id="ProductID">' + gJson[i].ProductID + '</td>';
lineHtml += ' <td id="ProductNo">' + gJson[i].ProductNo + '</td>';
lineHtml += ' <td id="Qty">' + gJson[i].Qty + '</td>';
lineHtml += ' <td id="InQty"><input type="text" name="InQty" id="InQty' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].InQty + '" /></td>';

lineHtml += ' <td id="PriceTax"><input type="text" name="PriceTax" id="PriceTax' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].PriceTax + '" /></td>';
lineHtml += ' <td id="ItemSum" ><input type="text" name="ItemSum" id="ItemSum' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].ItemSum + '" /></td>';
lineHtml += ' <td id="BatchNo"><input type="text" name="BatchNo" id="BatchNo' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].BatchNo + '" /></td>';
 lineHtml += '    </tr>';

如果我们用js组合了这样的一个table,想要获取其中的值,怎么获取呢?
我们可以看到里边有两种组合方式:

1.方式一: 直接绑定型

lineHtml += ' <td id="ProductNo">' + gJson[i].ProductNo + '</td>';

此时要获取ProductNo的值:

 var tableObj = document.getElementById('tb');

 var ProductNo = tableObj.rows[i + 1].cells["ProductNo"].innerText;

2.方式二:控件输入型

******* type=“text”********

lineHtml += ' <td id="BatchNo"><input type="text" name="BatchNo" id="BatchNo' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].BatchNo + '" /></td>';

此时要获取我们输入的BatchNo的值: 

var BatchNo = tableObj.rows[i + 1].cells.BatchNo.childNodes["0"].value;

********type=“checkbox”*******

lineHtml += ' <td ><input type="checkbox" name="chk" id="chk' + gIndex + '" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" value="1" /></td>';

此时我们要获取checkbox是否被选中

 var chkList = $("[name='chk']");

for (var i = 0; i < chkList.length; i++) {
var isChecked = chkList[i].checked;
if (isChecked == true) {

  //你想干的事

}
}

******本文原创,禁止转载************

原文地址:https://www.cnblogs.com/menglin/p/7567906.html