问题描述
- 求助啊怎么将数据库里读出的价格gprice显示到这个购物车的jsp页面中
-
这是我的代码,但是不会修改价格那个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>
解决方案二:
你不是已经读出数据赋值给label了,要怎么改?
时间: 2025-01-02 17:13:33