• ajax使用


    2015/8/25 19:07:38
    闵建军 2015/8/25 19:07:38


    21:24:29
    闵建军 2015/9/16 21:24:29
    private static final long serialVersionUID = -5156663215442050559L;

    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");
    response.setContentType("textml;charset=utf-8");

    int cart_id =Integer.parseInt(request.getParameter("cart_id"));
    int item_num = Integer.parseInt(request.getParameter("item_num"));


    String sql = "update t_v_cart set item_num =? where cart_id=?";
    Connection conn =DBUtil.getConnection();
    PreparedStatement ps =null;
    ResultSet rs =null;

    try {
    ps =conn.prepareStatement(sql);

    ps.setInt(1, item_num);
    ps.setInt(2, cart_id);
    ps.executeUpdate();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    DBUtil.close(conn, ps, rs);
    }
    PrintWriter out =response.getWriter();
    out.print("ok");
    out.flush();
    out.close();
    return;
    }

    闵建军 2015/9/16 21:24:49
    function ajax_get_cart_info(){
    $.ajax({
    type:"post",
    url:"card/ajaxgetcartinfo.action",
    dateType:"json",
    async:"true",//异步 false是同步
    success:function(data){

    if(data=="no"){

    window.location.href="user/login.html";
    return;
    }
    $("#tbody").html("");
    var html_op=""

    var vals = eval(data);

    for(var i=0;i<vals.length;i++){
    var price = vals[i].v_price;
    var count =vals[i].item_num;
    var cost = price*count;

    html_op =html_op+"<tr>" +
    "<td><input type='checkbox' name='item'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>" +
    "<td><a href='#' class='cart_product_img'><img alt='加载中...' src='img_v/"+vals[i].v_img+"' width='80'></a></td>" +
    "<td><a href='#'>"+vals[i].v_title+"</a></td><td>¥<span id='"+vals[i].cart_id+""+"price"+"'>"+vals[i].v_price+"</span>元</td>"
    + "<td><a href='javascript:void(0);' onclick='ajaxdecreasebuynum("+vals[i].cart_id+");' class='calc_down calc'>-</a>"
    + " <input type='text' value='"+vals[i].item_num+"' class='cart_buy_num' id='"+vals[i].cart_id+"' onkeyup='ajaxmodifybuynum("+vals[i].cart_id+","+vals[i].has_sum+");'>"
    + " <a href='javascript:void(0);' class='calc_up calc' onclick='ajaxincreasebuynum("+vals[i].cart_id+","+vals[i].has_sum+");'>+</a>"
    + "</td>"
    + "<td>"
    + " <span style='font-size:14px' id='"+vals[i].cart_id+""+"cost"+"'>"+ cost+"元</span>"
    + "</td>"
    + "<td>"
    + " <a href='#'>移除</a>"
    + "</td>"
    +"<span style='display:none'>"+vals[i].v_id+" </span>"
    +"</tr>"
    }
    $("#tbody").html(html_op);
    }
    });

    闵建军 2015/9/16 21:24:59
    //对购物车中的减数量进行数据库同步
    function ajaxdecreasebuynum(num){
    var count =$("#"+num+"").val();
    var price =$("#"+num+""+"price").text();
    if(count==1||count ==""){
    $("#"+num+"").val("1");
    count =1;
    }else{
    count--;
    $("#"+num+"").val(count);

    $.ajax({
    type:"post",
    url:"cart/ajaxdecreaseitemnum.action",
    data:{"cart_id":num,"item_num":count},
    dateType:"html",
    async:"true",//异步 false是同步
    success:function(data){
    if(data=="ok"){
    $("#"+num+""+"cost").text(price*count+"元");
    }
    }

    });
    }
    }
    //对购物车中的加数量进行数据库同步
    function ajaxincreasebuynum(num,has_sum){
    var count =$("#"+num+"").val();
    var price =$("#"+num+""+"price").text();
    if(count==""){
    $("#"+num+"").val("1");
    }else if(count>=has_sum){
    $("#"+num+"").val(has_sum);
    }else if(count<has_sum){
    count++;
    $("#"+num+"").val(count);

    $.ajax({
    type:"post",
    url:"cart/ajaxdecreaseitemnum.action",
    data:{"cart_id":num,"item_num":count},
    dateType:"html",
    async:"true",//异步 false是同步
    success:function(data){
    if(data=="ok"){
    $("#"+num+""+"cost").text(price*count+"元");
    }
    }
    });
    }
    }
    //在数量框中输入数量 更改数据库
    function ajaxmodifybuynum(num,has_sum){
    var count =$("#"+num+"").val();
    var price =$("#"+num+""+"price").text();
    if(count==""){
    $("#"+num+"").val("0");
    $("#"+num+""+"cost").text("0元");
    }
    if(count>has_sum){
    $("#"+num+"").val(has_sum);
    $("#"+num+""+"cost").text(price*has_sum+"元");
    }else{
    $("#"+num+"").val(count);
    $.ajax({
    type:"post",
    url:"cart/ajaxdecreaseitemnum.action",
    data:{"cart_id":num,"item_num":count},
    dateType:"html",
    async:"true",//异步 false是同步
    success:function(data){
    if(data=="ok"){
    $("#"+num+""+"cost").text(price*count+"元");
    }
    }
    });
    }
    }

  • 相关阅读:
    博客园装修行动—我设计的新首页
    按作者名称查看评论
    [新功能]通过RSS阅读收藏夹
    [公告]首页小调整
    时间的格式化
    [公告]将读书心得区改成技术书评区
    [汇报]昨天晚上博客园的程序又出现了问题
    [公告]博客园已经成功迁移至新服务器
    [好消息]博客园乔迁新居
    [公告]博客园程序升级
  • 原文地址:https://www.cnblogs.com/zy19930408/p/4868969.html
Copyright © 2020-2023  润新知