這是我的代碼,但是不會修改價格那個label,應該將我自己數據庫的數據怎麼貼進去,當我點擊+或者-來選擇數量的時候,後面的價格會跟著變,總價也會跟著變呢
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="Db.Db"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<title>購物車</title>
<link href="css/index.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.1.4.2-min.js"></script>
<script type="text/javascript" src="js/Calculation.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//jquery特效制作復選框全選反選取消(無插件)
// 全選
$(".allselect").click(function () {
$(".gwc_tb2 input[name=newslist]").each(function () {
$(this).attr("checked", true);
// $(this).next().css({ "background-color": "#3366cc", "color": "#ffffff" });
});
GetCount();
});
//反選
$("#invert").click(function () {
$(".gwc_tb2 input[name=newslist]").each(function () {
if ($(this).attr("checked")) {
$(this).attr("checked", false);
//$(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
} else {
$(this).attr("checked", true);
//$(this).next().css({ "background-color": "#3366cc", "color": "#000000" });
}
});
GetCount();
});
//取消
$("#cancel").click(function () {
$(".gwc_tb2 input[name=newslist]").each(function () {
$(this).attr("checked", false);
// $(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
});
GetCount();
});
// 所有復選(:checkbox)框點擊事件
$(".gwc_tb2 input[name=newslist]").click(function () {
if ($(this).attr("checked")) {
//$(this).next().css({ "background-color": "#3366cc", "color": "#ffffff" });
} else {
// $(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
}
});
// 輸出
$(".gwc_tb2 input[name=newslist]").click(function () {
// $("#total2").html() = GetCount($(this));
GetCount();
//alert(conts);
});
});
//******************
function GetCount() {
var conts = 0;
var aa = 0;
$(".gwc_tb2 input[name=newslist]").each(function () {
if ($(this).attr("checked")) {
for (var i = 0; i < $(this).length; i++) {
conts += parseInt($(this).val());
aa += 1;
}
}
});
$("#shuliang").text(aa);
$("#zong1").html((conts).toFixed(2));
$("#jz1").css("display", "none");
$("#jz2").css("display", "block");
}
</script>
</head>
<%
String[] strgid = request.getParameterValues("addOrders");
Connection conn = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
conn = Db.getconn();
stmt = conn.createStatement();
%>
<body>
<div class="gwc" style=" margin:auto;">
<table cellpadding="0" cellspacing="0" class="gwc_tb1">
<tr>
<td class="tb1_td1"><input id="Checkbox1" type="checkbox" class="allselect"/></td>
<td class="tb1_td1">全選</td>
<td class="tb1_td3">商品</td>
<td class="tb1_td5">數量</td>
<td class="tb1_td6">單價</td>
<td class="tb1_td7">操作</td>
</tr>
</table>
<!---商品加減算總數---->
<script type="text/javascript">
$(function () {
var t = $("#text_box1");
$("#add1").click(function () {
t.val(parseInt(t.val()) + 1)
setTotal(); GetCount();
})
$("#min1").click(function () {
t.val(parseInt(t.val()) - 1)
setTotal(); GetCount();
})
function setTotal() {
$("#total1").html((parseInt(t.val()) * 9).toFixed(2));
$("#newslist-1").val(parseInt(t.val()) * 9);
}
setTotal();
})
</script>
<table cellpadding="0" cellspacing="0" class="gwc_tb2">
<%
for(int i = 0;i<strgid.length;i++) {
int gid = Integer.parseInt(strgid[i]);
rs = stmt.executeQuery("select * from goods where gid ="+gid);
while(rs.next()) {
%>
<tr>
<td class="tb2_td1"><input type="checkbox" value="1" name="newslist" id="newslist-1" /></td>
<td class="tb2_td2"><a href="#"><img src="<%=rs.getString("gpicture") %>"/></a></td>
<td class="tb2_td3"><a href="#"><%=rs.getString("gname") %></a></td>
<td class="tb1_td5">
<input id="min1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="-" />
<input id="text_box1" name="" type="text" value="1" style=" width:30px; text-align:center; border:1px solid #ccc;" />
<input id="add1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="+" />
</td>
<td class="tb1_td6"><label id="total1" name="total2" class="tot" " style="color:#ff5500;font-size:14px; font-weight:bold;" ><%=rs.getString("gprice") %></label></td>
<td class="tb1_td7"><a href="#">刪除</a></td>
</tr>
<%}
}%>
</table>
<!---商品加減算總數---->
<script type="text/javascript">
$(function () {
var t = $("#text_box2");
$("#add2").click(function () {
t.val(parseInt(t.val()) + 1)
setTotal(); GetCount();
})
$("#min2").click(function () {
t.val(parseInt(t.val()) - 1)
setTotal(); GetCount();
})
function setTotal() {
$("#total2").html((parseInt(t.val()) * 8).toFixed(2));
$("#newslist-2").val(parseInt(t.val()) * 8);
}
setTotal();
})
</script>
<!---總數---->
<script type="text/javascript">
$(function () {
$(".quanxun").click(function () {
setTotal();
//alert($(lens[0]).text());
});
function setTotal() {
var len = $(".tot");
var num = 0;
for (var i = 0; i < len.length; i++) {
num = parseInt(num) + parseInt($(len[i]).text());
}
//alert(len.length);
$("#zong1").text(parseInt(num).toFixed(2));
$("#shuliang").text(len.length);
}
//setTotal();
})
</script>
<table cellpadding="0" cellspacing="0" class="gwc_tb3">
<tr>
<td class="tb1_td1"><input id="checkAll" class="allselect" type="checkbox" /></td>
<td class="tb1_td1">全選</td>
<td class="tb3_td1">
<input id="invert" type="checkbox" />反選
<input id="cancel" type="checkbox" />取消
</td>
<td class="tb3_td2">已選商品 <label id="shuliang" style="color:#ff5500;font-size:14px; font-weight:bold;">0</label> 件</td>
<td class="tb3_td3">合計(不含運費):<span>¥</span><span style=" color:#ff5500;"><label id="zong1" style="color:#ff5500;font-size:14px; font-weight:bold;"></label></span></td>
<td class="tb3_td4"><span id="jz1">結算</span><a href="#" style=" display:none;" class="jz2" id="jz2">結算</a></td>
</tr>
</table>
</div>
</body>
</html>
不能用id選擇器,你的id都重復了,要用屬性選擇器,你那列是單價,應該增加一列,作為此列的小計,而不是直接覆蓋單價那列.
<table cellpadding="0" cellspacing="0" class="gwc_tb1">
<tr>
<td class="tb1_td1"><input id="Checkbox1" type="checkbox" class="allselect"/></td>
<td class="tb1_td1">全選</td>
<td class="tb1_td3">商品</td>
<td class="tb1_td5">數量</td>
<td class="tb1_td6">單價</td>
<td class="tb1_td6">小計</td><!------------增加小計列,不要直接設置單價那裡------------>
<td class="tb1_td7">操作</td>
</tr>
</table>
<tr>
<td class="tb2_td1"><input type="checkbox" value="1" name="newslist" id="newslist-1" /></td>
<td class="tb2_td2"><a href="#"><img src="<%=rs.getString("gpicture") %>"/></a></td>
<td class="tb2_td3"><a href="#"><%=rs.getString("gname") %></a></td>
<td class="tb1_td5">
<input id="min1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="-" />
<input id="text_box1" name="" type="text" value="1" style=" width:30px; text-align:center; border:1px solid #ccc;" />
<input id="add1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="+" />
</td>
<td class="tb1_td6"><label id="total1" name="total2" class="tot" " style="color:#ff5500;font-size:14px; font-weight:bold;" ><%=rs.getString("gprice") %></label></td>
<td class="tb1_td6"></td><!------------增加小計列,不要直接設置單價那裡------------>
<td class="tb1_td7"><a href="#">刪除</a></td>
</tr>
js代碼的更改
<!---商品加減算總數---->
<script type="text/javascript">
$(function () {
$("input[id='add1']").click(function (e,init) {
var t=$(this).prev();
if(!init){//點擊按鈕,不是初始化
var v=parseInt(t.val(),10)||0;v++;
t.val(v)
}
setTotal(t); GetCount();
}).trigger('click',true)//初始化
$("input[id='min1']").click(function (e,init) {
var t=$(this).next();
if(!init){//點擊按鈕,不是初始化
var v=parseInt(t.val(),10)||0;v--;if(v<0)v=0;
t.val(v)
}
setTotal(t); GetCount();
}).trigger('click',true)//初始化
function setTotal(t) {
var tds=t.closest('tr').find('td')//得到此行所有td對象
var td=tds.eq(5);//得到小計的td
var lb=parseInt(tds.eq(4).find('label').text())||0//得到單價容器
var sum=(parseInt(t.val()) * lb).toFixed(2);//小計
td.html(sum);
tds.eq(0).find('input').val(sum);//設置你checkbox的值
}
setTotal();
})
</script>