您的位置:首页 > Web前端 > JavaScript

SM-添加账单(默认列出某供应商全部商品)-通过checkbox多选框和ajax实现账单添加时不定量商品项的servlet传值

2016-12-25 19:21 639 查看
//在JSP页面循环输出指定供应商的商品,通过多选框选择商品

<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);//插入账单明细
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet ajax checkbox jsp