如何添加复选框的值以获得总价
how to add values of checkbox to get the total price
我正在创建一个页面,其中包含一些服务及其价格。因此,当用户通过选中旁边的复选框来选择服务时,价格应该被添加到总价格中。此外,如果他未选中该复选框,则该服务的价格将从总额中减去。
我已经尝试了很多方法,但都没有得到结果。
这是我的代码。谁能帮我做我想做的事?
<html>
<head>
<title> new bill</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
include 'config.php';
include 'searchbill.php';
$query = "SELECT * FROM service";
$result = mysqli_query($con, $query);
if ($result){
?>
<center> <font size="5" face="WildWest" color="#4EE2EC"> Services</font></center>
<table align="center" class="imagetable" accept-charset="utf-8">
<tr style="background-color: #80E6CC;color:white;" aligh="center">
<th>number</th>
<th>service name</th>
<th>employee</th>
<th>price</th>
<th>check</th>
</tr>
<?php
$id = 1;
while ($row = mysqli_fetch_array($result)){
echo "<tr><td align='center'>" . $id++;
echo "<td align='center'>" . $row["sname"] . "</td>";
echo "<td align='center'>";
$emb = "SELECT * FROM employee";
$resultemb = mysqli_query ($con, $emb);
if($resultemb){?>
<select name="ename" STYLE="margin-right: 83px; alignment-adjust: middle; font-family: Verdana; font-weight: bold; font-size: 12px; color: #379EA5; " >
<?php while ($row1 = mysqli_fetch_array($resultemb)){
for($i=1; $i<= count($row1["ename"]);$i++){
$select = $row1["ename"];
echo $select;
echo "<br/>";
echo "<option value='$select'>".$select."</option>";
}
} ?>
</select>
<?php
}
echo "<td align='center'>" . $row["sprice"] . "</td>";
echo "<td align='center'>";?>
<input type="checkbox" name="serprice" value="<?php $row['sprice']; ?>">
<?php
echo "</td></tr>";
?>
<?php
}
}
?>
</body>
</html>
试试这个,演示小提琴
$('input[type="checkbox"]').change(function(){
var totalprice = 0;
$('input[type="checkbox"]:checked').each(function(){
totalprice= totalprice + parseInt($(this).val());
});
$('#price').val(totalprice);
});
计算选中输入的值,使用:checked。
试试这个应该可以工作。下面是这个的jsfiddle http://jsfiddle.net/sushilbharwani/QCndj/
<>之前
$('input[type=checkbox]').click(function(){
total = 0;
$('input[type=checkbox]:checked').each(function(){
total = total + parseInt($(this).val());
});
alert(total);
});
相关文章:
- Bootstrap多选-在复选框后添加图像
- 通过选中和取消选中复选框来添加和删除数组中的值
- 如何在使用onclick函数选中复选框时添加或减去值
- 如何在复选框后添加文本
- 如何将jquery数组添加到html表单中,以将复选框动态添加到对话框中
- 将复选框动态添加到 jquery dilaogue 框中
- 从复选框中添加/减去值
- 在 extjs4 中编辑时需要对齐复选框、添加多条记录并查看日期归档
- 将可绑定复选框列添加到 Internet Explorer 中的网格
- 键:选中复选框时添加一个值
- 向复选框属性添加数字并限制选中复选框的数量
- php在选中复选框时添加文本框不会发布
- 如何仅在选中复选框时添加值
- 如何在Quit复选框中添加事件
- 复选框 - 选中添加文本,取消选中删除文本区域中的原始文本
- 在复选框上添加动态行和数据单击
- 在Javascript中从复选框中添加/删除字段值
- 在显示/隐藏复选框中添加动画
- Javascript:在现有的复选框旁边添加一个额外的复选框
- 在隐藏复选框上添加更改事件