我有一个网格,它在PHP中显示来自MYSQL数据库的数据
我还提供了一个复选框,如果用户选中了该复选框,则应该添加选中的值并显示在底部。
这是php网格:
<form id='myform' method='post' action=''>
<table id='mytable'>
<?php
$sql_getData = "SELECT * FROM MYTABLE";
$result_getData = mysql_query();
while($arr_data = mysql_fetch_array($sql_getData))
{
?>
<tr>
<td><?php echo $arr_data['payment_id'] ?></td>
<td><?php echo $arr_data['payments'] ?></td>
<td><input type='checkbox' name='mycheckbox' class='mycheckbox'></td>
<tr>
<?php
}
?>
<tr><td>Total Payments</td><td>TOTAL_VALUE</td></tr>
</table>
</form>
编辑:提供由Reigel建议的输出**
**预期产量:
如何使用jquery显示TOTAL_VALUE
更新的代码将循环中的所有值相加,因为它不应该...! :
<form id="myform" method="post" action="">
<table id="mytable">
<?php
$sql_getData = "SELECT * FROM MYTABLE";
$result_getData = mysql_query();
while($arr_data = mysql_fetch_array($sql_getData)){
echo '
<tr>
<td>'.$arr_data['payment_id'].'</td>
<td>'.$arr_data['payments'].'</td>
<td><input type="checkbox" name="mycheckbox" class="mycheckbox" data-payment="'.$arr_data['payments'].'"></td>
<tr>'
}
?>
<tr>
<td>Total Payments</td>
<td id="tp">0.00</td>
</tr>
</table>
</form>
<script>
$(".mycheckbox").change(function(){
var payment = 0;
$(".mycheckbox:checked").each(function(){
payment += parseFloat($(this).attr("data-payment"));
});
$("#tp").html("<p>"+payment.toFixed(2)+"</p>");
});
</script>
最终更新的代码在Andy gee的指导下非常完美:
<form id="myform" method="post" action="">
<table id="mytable">
<?php
$sql_getData = "SELECT * FROM MYTABLE";
$result_getData = mysql_query();
while($arr_data = mysql_fetch_array($sql_getData)){
echo '
<tr>
<td>'.$arr_data['payment_id'].'</td>
<td>'.$arr_data['payments'].'</td>
<td><input type="checkbox" name="mycheckbox" class="mycheckbox" data-payment="'.$arr_data['payments'].'"></td>
<tr>'
}
?>
<tr>
<td>Total Payments</td>
<td id="tp">0.00</td>
</tr>
</table>
</form>
<script>
$(".mycheckbox").change(function(){
var payment = 0;
$(".mycheckbox").each(function(){
payment += $(this).prop("checked") ? parseFloat($(this).attr("data-payment")) : 0;
});
$("#tp").html("<p>"+payment.toFixed(2)+"</p>");
});
</script>
最佳答案
如果我理解正确,则应该为您执行此操作-假设$arr_data['payments']
包含付款金额
<?
$exch_rate = 1.2;
?>
<form id="myform" method="post" action="">
<table id="mytable">
<?php
$sql_getData = "SELECT * FROM MYTABLE";
$result_getData = mysql_query();
while($arr_data = mysql_fetch_array($sql_getData)){
echo '
<tr>
<td>'.$arr_data['payment_id'].'</td>
<td>'.$arr_data['payments'].'</td>
<td><input type="checkbox" name="mycheckbox" class="mycheckbox" data-payment="'.$arr_data['payments'].'"></td>
<tr>'
}
?>
<tr>
<td>Total Payments</td>
<td id="tp">0.00</td>
<td id="anotherid">0.00</td>
</tr>
</table>
</form>
<script>
$(".mycheckbox").change(function(){
var exch_rate = <?=$exch_rate;?>;
var payment = 0;
$(".mycheckbox").each(function(){
payment += $(this).prop("checked") ? parseFloat($(this).attr("data-payment")) : 0;
});
$("#tp").html("<p>"+payment.toFixed(2)+"</p>");
payment *= exch_rate;
$("#anotherid").html("<p>"+payment.toFixed(2)+"</p>");
});
</script>
关于php - 如何对jquery中选中字段的值求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24095798/