我有一个html表格
<table id="tblProduct" class="table table-bordered">
<thead>
<tr>
<form action="" method="post">
<td><input type="hidden" class='hidenFeild' name="id" value="<?php echo $product_ids; ?>" /></td>
<td><?php echo $odr; ?></td>
<td><?php echo $rows_pro['product_title']; ?></td>
<td><span class="fa fa-gbp"></span><?php echo $rows_pro['amount']; ?></td>
<td><input type="text" name="quantity" id="spinner<?php echo $rows_shopings['product_id']; ?>" class="spinnerExample"/></td>
<td>
<button type="submit" onclick="return confirm('Are you sure you want to delete?');" class="btn btn-primary" name="btn_dlt">
<i class="fa fa-trash icon-white"></i> Delete
</button>
</td>
</form>
</tr>
</thead>
<tbody>
...
我在表中有许多产品,我必须获得第一个 td 和具有产品数量的 td 的值。
到目前为止我尝试过的是:
$('#tblProduct > tbody > tr').each(function() {
$('input[type="hidden"][name="id"]').each(function() {
alert($(this).val());
});
$('input[type="text"][name="quantity"]').each(function(){
alert($(this).val());
});
});
但我没有得到想要的结果。谁能帮帮我?
最佳答案
您的代码中缺少一些东西,我已经修改了您的代码以遍历表格并列出所有产品详细信息。
JQUERY 代码:
$('#tblProduct > tbody tr').not(':last').each(function(index) {
var value = $(this).find('input[type="hidden"][name="id"]').val();
var qty = $(this).find('input[type="text"][name="quantity"]').val();
console.log("Value:"+ value +", Quantity:"+ qty);
});
注意:上面的脚本会忽略最后一行
关于jquery - 遍历表行得到其两个TD的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171590/