Javascript 遍历动态生成的表并访问其中的td值
这是我在控制器中的代码Javascript 遍历动态生成的表并访问其中的td值,javascript,php,jquery,codeigniter,Javascript,Php,Jquery,Codeigniter,这是我在控制器中的代码 function get_grproduct(){ $goodreceipt_id=$_POST['goodreceipt_id']; if($goodreceipt_id!=''){ //$post_array['cart']=''; $res = $this->db->query("select * from phppos_productdetails WHERE goodreceipt_id='$g
function get_grproduct(){
$goodreceipt_id=$_POST['goodreceipt_id'];
if($goodreceipt_id!=''){
//$post_array['cart']='';
$res = $this->db->query("select * from phppos_productdetails WHERE
goodreceipt_id='$goodreceipt_id'");
?>
<tr>
<th>Particulars</th>
<th>Packing</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
<!--<th>Action</th>-->
</tr>
<?php
$i=0;
foreach($res->result() as $row ){
$query = $this->db->query("SELECT product_name FROM phppos_product WHERE
product_id='".$row->product_id."'");
foreach($query->result() as $row1 ){
$product_name=$row1->product_name;
}
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing'/></td>";
echo "<td><input type='text' id='quantity' style='width:80%;' readonly=''
value='".$row->quantity."'/></td>";
echo "<td><input type='text' style='width:80%;' id='rate'/></td>";
echo "<td><input type='text' style='width:100%;' id='amount' readonly=''/></td>";
echo "</tr>";
$i++;
}
}
}
所以我的问题是,它只为第一条记录将amt变量值分配给amount id。那么,我如何使用loop in bur函数,以便分配每条记录的金额并获得总金额???您需要使用类而不是
id
,因为它必须是唯一的。
另外,最新版本的jquery中没有使用as。
现在,你的代码看起来像
$("#cart_details").on('blur','.rate', function(){
var rate=this.value;
var $tr=$(this).closest('tr');
var qty =$tr.find('.quantity').val(); // use quantity class not id
var amt=parseInt(rate)* parseInt(qty);
$tr.find(".amount").val(amt); // use amount class not id
});
在你的PHP代码中
.......
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing'/></td>";
echo "<td><input type='text' class='quantity' style='width:80%;' readonly='' value='".$row->quantity."'/></td>";
echo "<td><input type='text' style='width:80%;' class='rate'/></td>";
echo "<td><input type='text' style='width:100%;' class='amount' readonly=''/></td>";
echo "</tr>";
.......
。。。。。。。
回声“;
回显“$product_name.”;
回声“;
回声“;
回声“;
回声“;
回声“;
.......
我会根据需要更改您的代码,但它会为每条记录分配相同的金额。您是否可以显示在线演示,或者是否有我可以看到的URL。sry..我不能..但我可以向您显示我更改的代码..查看上面的编辑您使用live()的方式错误,请改为使用us ON()。此外,请参阅浏览器控制台中的任何错误。使用$(“#cart_details”).on('blur','.rate',function(){
而不是$(“#cart_details”).live('blur','.rate',function(){
var rate=$(this).val()非var rate=$('rate').val()
和var tr$(this)
首先尝试我的代码,因为我知道live不推荐使用,但如果我在事件中使用,那么一切都会停止工作。我包括jquery 1.11.2.min.js文件。
.......
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing'/></td>";
echo "<td><input type='text' class='quantity' style='width:80%;' readonly='' value='".$row->quantity."'/></td>";
echo "<td><input type='text' style='width:80%;' class='rate'/></td>";
echo "<td><input type='text' style='width:100%;' class='amount' readonly=''/></td>";
echo "</tr>";
.......