cshmicky 2017-01-10 20:00 采纳率: 0%
浏览 832

前端相关问题——总价和删除勾选项功能实现不了

 我想实现一个购物车功能,遇到的问题是总价和删除勾选项功能实现不了。请求各位大神帮忙看看

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table th, table td {
border-bottom: 1px solid #aaa;
padding: 8px 15px;
text-align: center;
}
thead > tr {
background: #6a6;
}
tbody > tr:nth-child(even) {
background: #efe;
}
tbody tr:nth-child(2){
line-height: 40px;
}
tfoot td {
text-align: right;
}
/*a {
text-decoration: none;
color: #000;
}*/

p{
float:right;
}
ul{
margin: 0 auto;
}
li{
list-style: none;
float:left;
margin:10px; 
}
</style>
</head>
<body>
<h1>练习:jQuery DOM操作和事件处理</h1>
<h3>购物车计算器</h3><br>
<button id="btAdd">添加新商品</button><br><br>
<table id="cart">
<thead>
<tr>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td id="price">3.5</td>
<td>
<input class=" btn1" type="button" id="subtract" value="-"/>
<input class=" input " type="button"id="num" value="1"/>
<input class=" btn2" type="button" id="add" value="+"/>
</td>
<td id="sum">7.0</td> 
<td><input name="subBox" class="listCheckBox" type="checkbox"> </td>
</tr>

</tbody>
</table>
<p id="delet">删除勾选项</p>
<br />
<ul>
<li >
<lable>全选</lable>
<input type="checkbox" id="checkAll">
</li>
<li>购物车总金额:<span id="total">3.5</span></li>
</ul>
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
var p=$('#price').html();
//点击加按钮
$('#add').click(function(){
var n=$(this).parent().find('#num'); 
n.val(parseInt(n.val())+1);
total();
});
//点击减号按钮
$('#subtract').click(function(){
var n=$(this).parent().find('#num');
n.val(parseInt(n.val())-1);
if(parseInt(n.val())<1){ 
n.val(1);
}
total();
getTotal();
});
//小计
function total(){
var s=0;
s+=parseFloat(p*$('#num').val());
$("#sum").html(s); 
}
total();
//合计
function getTotal(){
var t=0;
var sum=$('#sum').innerHTML;
t+=sum;
$('#total').html(t);
}
getTotal();
//全选与反选
$("#checkAll").click(function() {
$('input[name="subBox"]').attr("checked",this.checked); 
});
var $subBox = $("input[name='subBox']");
$subBox.click(function(){
$("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
});
//删除勾选项
$('#delet').click(function(){
var ch=$subBox.attr('checked');
$('#delet').prev().remove();
});
</script>

</body>
</html>
  • 写回答

3条回答 默认 最新

  • 林家小米 2017-01-11 01:08
    关注

    不是特别清楚你想表达什么,大概理解为勾选的按钮不能选中或者取消,楼主可以参考下我的blog

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误