SM-添加账单(默认列出某供应商全部商品)-通过checkbox多选框和ajax实现账单添加时不定量商品项的servlet传值
2016-12-25 19:21
639 查看
//在JSP页面循环输出指定供应商的商品,通过多选框选择商品
//在JS中执行ajax,通过条件循环,将checkbox选中项所在的table-tr中的一系列值装入ajax的参数中
//对应servlet中,通过getParameterValues()方法,取得同一name的多个值
<form id="form1" name="form1" method="get" action="bill.do" onsubmit="return checkit();"> <input type="hidden" name="opr" value="doAdd"><br> <span style="font-size: 20px;color: #4169E1;">所属供应商:<input type="text" value="${pname}" readonly="readonly"/></span> <div class="content"> <table class="list"> <tr> <td width="30" height="29"><div class="STYLE1" align="center">选择</div> </td> <td width="80"><div class="STYLE1" align="center">商品编号</div> </td> <td width="60"><div class="STYLE1" align="center">商品名称</div> </td> <td width="60"><div class="STYLE1" align="center">商品进价</div> </td> <td width="60"><div class="STYLE1" align="center">数量</div> </td> </tr> <c:forEach items="${glist}" var="g"> <tr> <td><div class="STYLE1" align="center"> <input type="checkbox" name="gid" value="${g.id}" /> </div> </td> <td><div class="STYLE1" align="center">${g.id}</div> </td> <td><div class="STYLE1" align="center">${g.goodsName}</div> </td> <td class="price"><div class="STYLE1" align="center">${g.inprice}</div> </td> <td class="num"><div class="STYLE1" align="center"> <input type="text" name="num" /> </div> </td> </tr> </c:forEach> </table> </div> <div class="buttons"> <input type="button" id="button" onclick="submitBill();" value="提交账单" class="input-button" /> <input type="button" name="button" id="button" onclick="window.location='wellcome.html';" value="取消" class="input-button" /> </div> </form>
//在JS中执行ajax,通过条件循环,将checkbox选中项所在的table-tr中的一系列值装入ajax的参数中
function submitBill(){ var formUrl="bill.do"; //var $ck= var count=1; var a = '<%=request.getAttribute("pid")%>' ; $("input[name='gid']:checked").each(function(x,obj){ alert(obj); var $ckobj=$(obj); var v=$ckobj.val(); var price=$ckobj.parent().parent().siblings(".price").children().text();//价格 var num=$ckobj.parent().parent().siblings(".num").children().children().val();//数量 console.log(price*num); var amount=(price*num).toFixed(2); if(count==1){ formUrl+="?opr=doAdd"; } formUrl+="&gid="+v; formUrl+="&num="+num; formUrl+="&amount="+amount; count++; }); formUrl+="&pid="+a; alert(formUrl); window.location.href=formUrl; }
//对应servlet中,通过getParameterValues()方法,取得同一name的多个值
String pidStr=request.getParameter("pid"); int pid=0; if(pidStr!=null&&!pidStr.equals("")){ pid=Integer.parseInt(pidStr); } if(pid==0){ response.sendRedirect(request.getContextPath()+"/billmanage/admin_bill_list.jsp"); } String[] gidStr=request.getParameterValues("gid"); String[] numStr=request.getParameterValues("num"); String[] amount=request.getParameterValues("amount"); double totalAmount=0; for(String v:amount){ totalAmount+=Double.parseDouble(v); } Bill bill=new Bill(); bill.setProviderId(pid); bill.setTotalAmount(totalAmount); int bid= billBiz.addBill(bill);//插入账单总表 for(int i=0;i<gidStr.length;i++){ BillItem bm=new BillItem(); bm.setGoodsId(Integer.parseInt(gidStr[i])); bm.setGoodsCount(Integer.parseInt(numStr[i])); bm.setAmount(Double.parseDouble(amount[i])); System.out.println(amount[i]); bm.setBillId(bid); //将总账单Id封装到账单详情对象里 billBiz.addBillItem(bm);//插入账单明细 }
相关文章推荐
- JSP/PHP基于Ajax的分页功能实现
- 开发阶段Jetty运行Jsp报错且响应空白
- Ajax
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- 使用Ajax实现异步用户名验证
- 使用ajax实现用户登录验证(升级版)
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- 再谈Jquery Ajax方法传递到action(补充)
- Dom在ajax技术中的作用说明
- jquery的checkbox,radio,select等方法小结
- AJAX实现瀑布流触发分页与分页触发瀑布流的方法
- jsp简单实现页面之间共享信息的方法
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在
- Apache Web让JSP“动”起来
- PHP+jquery+ajax实现分页
- jQuery根据ID获取input、checkbox、radio、select的示例