Checkbox框全选操作,form表单提交与jquery ajax提交两种处理方式

//1.jquery ajax
<
script type="text/javascript"> $(function(){ var basePath = $("#basePath").val(); //上架操作 $("#upAll").click(function(){ var chk_value =""; $('input[name="ckID"]:checked').each(function(){ chk_value += $(this).val()+","; }); if(chk_value == ""){ alert("请选择需要上架的项!!"); return; }else{ if(!confirm("确定上架吗?")){ return; } } var addNetworkId = $("#addNetworkId").val(); //alert("chk_value:" + chk_value); var dataString = {"actionName" : "up","addNetworkId" : addNetworkId, "chk_value" : chk_value}; var encoded = $.toJSON( dataString ); //alert("encoded:"+encoded); $.ajax({ type: "POST", url: basePath+"partsInfoKuAjaxAction", data: encoded, dateType:"json", success: function(data) { if(data.success == 1){ alert("商品上架成功!"); location.reload(true); //刷新,重新加载 }else if(data.success == 0){ alert("商品上架失败!"); } }, error: function(xhr) { //中间发生异常,具体查看xhr.responseText alert("error:" + xhr.responseText); } }); }); }); </script>

 

2.servlet处理,doPost方法

/**输入*/

        String json = JsonUtils.readJSONString(request);

        log.info("删除Supply ID Json串: "+json);

        Map map = GsonUtils.parseData(json);

        String actionName = (String) map.get("actionName");

        String chk_value = (String) map.get("chk_value");

        String addNetworkId = (String) map.get("addNetworkId");

        if(actionName.equals("up")){

            String strUp[] = chk_value.split(",");

            boolean flag = false;

            Map mapPara;

            for (int i = 0; i < strUp.length; i++) {                 Integer id = Integer.parseInt(strUp[i]);

                mapPara = new HashMap();

                mapPara.put("networkInfoId", Integer.valueOf(addNetworkId));

                mapPara.put("updown", 0);

                mapPara.put("partsInfoId", id);

                flag = partnerMgrService.updateNetWorkInfoPartsInfo(mapPara);

            }             String strJson;

            if(flag){

                strJson = "{\"success\":\"1\"}";

            }else{

                strJson = "{\"success\":\"0\"}";

            }

            /**输出*/

            JsonUtils.writeJSONString(response, strJson);

            

        }

 

3.JsonUtils类

@Repository

public class JsonUtils {    

    public static String readJSONString(HttpServletRequest request) {

        StringBuffer json = new StringBuffer();

        String line = null;

        try {

            BufferedReader reader = request.getReader();

            while ((line = reader.readLine()) != null) {

                json.append(line);

            }

        } catch (Exception e) {

             //ln(e.toString());

        }

        return json.toString();

    }

    

    public static void writeJSONString(HttpServletResponse response,String strJson) throws IOException{

        //String strJson = "{\"success\":\"1\"}";

        response.setContentType("application/json; charset=utf-8");

        PrintWriter out = response.getWriter(); 



        out.print(strJson); 

        out.flush();

        out.close(); 

    }

}


4.form表单提交方式,post提交

String addNetworkId = request.getParameter("addNetworkId");

        String actionName = request.getParameter("actionName");

        if(actionName.equals("addOp")){

            String[] checked = request.getParameterValues("ckID");             String chkValue;

            String retailPrice;

            Map map;

            if(checked != null){

                 for(int j=0; j<checked.length; j++){

                     chkValue = checked[j];

                     retailPrice = request.getParameter("retailPrice"+chkValue);                      map = new HashMap();

                     map.put("partsInfoId", chkValue);                      map.put("retailPrice", retailPrice);

                     map.put("networkInfoId", Integer.valueOf(addNetworkId));

                     list.add(map); //添加到集合中来。

                 }

            }

            boolean flag = partnerMgrService.addNetWorkInfoPartsInfo(list);

            response.setContentType("text/html; charset=utf-8");

            PrintWriter out = response.getWriter();

            if (flag) {

                log.info("添加商品成功");

                out.print("<script>" + "alert('添加商品成功');"

                        + "window.location.href='" + Constants.ROOT

                        + "/partner/network/partskumgr.jsp';" + "</script>");

            } else {

                log.info("添加商品失败");

                out.print("<script>alert('添加商品失败');" + "window.location.href='"

                        + Constants.ROOT + "/partner/network/addpartsku.jsp';"

                        + "</script>");

            }

        }

 

你可能感兴趣的:(jQuery ajax)