我有一个代码。
var countRow = $('table#receiving-stock-view tr').length - 1;
var ar = new Array();
for(var iter=0;iter<countRow;iter++){
ar[iter] = $('div.modal table#receiving-stock-view tr#'+iter).find('input[name=rspid]').attr('id')
+","+$('div.modal table#receiving-stock-view tr#'+iter).find('input[name=rsqty]').val()
+","+$('div.modal table#receiving-stock-view tr#'+iter).find('input[name=rscost]').val()
+","+$('div.modal table#receiving-stock-view tr#'+iter).find('input[name=rssrp]').val();
console.log($('table#receiving-stock-view tr#'+iter).find('input[name=rsqty]').val());
console.log(ar[iter]);
}
但是,在循环期间,它始终只适用于第一行。它在其他行上随机失败。
然而,
for(var iter=0;iter<countRow;iter++){
ar[iter] = $('#receiving-stock-view tr#'+iter).find('input[name=rspid]').attr('id')
+","+$('#receiving-stock-view tr#'+iter).find('input[name=rsqty]').val()
+","+$('#receiving-stock-view tr#'+iter).find('input[name=rscost]').val()
+","+$('#receiving-stock-view tr#'+iter).find('input[name=rssrp]').val();
}
如果我没有指定要查找和保留 id 的元素,它会正确读取所有值。知道为什么会这样吗?这真的很奇怪。
编辑:我使用 ajax 添加了一个新行。整个表格都在一个模态类中。
<table id="receiving-stock-view">
<thead hidden="true" style="display: table-header-group;">
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Cost</th>
<th>SRP</th>
<th>Sub-Total</th></tr>
</thead>
<tbody>
<tr id="0">
<td><span><input type="text" value="HeadPhoneszs" name="rspid" id="1"></span></td>
<td><input type="text" value="1" name="rsqty" id="rsqty_id"></td>
<td><input type="text" value="1" name="rscost" id="rscost_id"></td>
<td><input type="text" value="1" name="rssrp" id="rssrp_id"></td></tr>
<tr id="1"><td><span><input type="text" value="vp" name="rspid" id="13"></span></td>
<td><input type="text" value="2" name="rsqty" id="rsqty_id"></td>
<td><input type="text" value="2" name="rscost" id="rscost_id"></td>
<td><input type="text" value="2" name="rssrp" id="rssrp_id"></td>
</tr>
</tbody>
</table>