from表单中checkbox的多选,ajax转入后台,后台接受

时间:2023-02-09 15:02:29

var check = [];//定义一个空数组
$("input[name='category']:checked").each(function(i){//把所有被选中的复选框的值存入数组
check[i] =$(this).val();
});
$.ajax({
type : "post",
url : "${ctx}/ask/ask/check",
data : {"check":check},
traditional:true,
datatype:"json",
success:function(dates){

},
error: function() {
alert("失败,请稍后再试!");
}
});

from表单中checkbox的多选,ajax转入后台,后台接受

<div id="check">
<label class="control-label">设备:</label>
<div class="controls">
<c:forEach var="list" items="${askc.eqc}" varStatus="">
<input type="checkbox" id="checkbox" name="category" value="${list.eqId }" />${list.eqName}<br>
</c:forEach>
</div>
</div>

from表单中checkbox的多选,ajax转入后台,后台接受

------------------------------------------------------

ajax 转入  traditional:true,必须写

后台接受

@ResponseBody
@RequestMapping(value = "check")
public static String[] check(HttpServletRequest request) {
ids=request.getParameterValues("check");
return ids;
}

ids=request.getParameterValues("check");

from表单中checkbox的多选,ajax转入后台,后台接受

前台

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>巡检任务成功管理</title>
<meta name="decorator" content="default"/>
<script type="text/javascript">
$(document).ready(function() {
//$("#name").focus();
$("#inputForm").validate({
submitHandler: function(form){
loading('正在提交,请稍等...');
var check = [];//定义一个空数组
$("input[name='category']:checked").each(function(i){//把所有被选中的复选框的值存入数组
check[i] =$(this).val();
});
$.ajax({
type : "post",
url : "${ctx}/ask/ask/check",
data : {"check":check},
traditional:true,
datatype:"json",
success:function(dates){

},
error: function() {
alert("失败,请稍后再试!");
}
});
form.submit();
},
errorContainer: "#messageBox",
errorPlacement: function(error, element) {
$("#messageBox").text("输入有误,请先更正。");
if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
error.appendTo(element.parent().parent());
} else {
error.insertAfter(element);
}
}
});
});
</script>
</head>
<body>
<ul class="nav nav-tabs">
<li><a href="${ctx}/ask/ask/">巡检任务成功列表</a></li>
<li class="active"><a href="${ctx}/ask/ask/form?id=${ask.id}">巡检任务成功<shiro:hasPermission name="ask:ask:edit">${not empty ask.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="ask:ask:edit">查看</shiro:lacksPermission></a></li>
</ul><br/>
<form:form id="inputForm" modelAttribute="ask" action="${ctx}/ask/ask/save" method="post" class="form-horizontal">
<form:hidden path="id"/>
<sys:message content="${message}"/>
<div class="control-group">
<label class="control-label">巡检任务名称:</label>
<div class="controls">
<form:input path="name" htmlEscape="false" maxlength="255" class="input-xlarge "/>
</div>
</div>
<div id="check">
<label class="control-label">设备:</label>
<div class="controls">
<c:forEach var="list" items="${askc.eqc}" varStatus="">
<input type="checkbox" id="checkbox" name="category" value="${list.eqId }" />${list.eqName}<br>
</c:forEach>
</div>
</div>
<br>
<div class="control-group">
<label class="control-label">巡检人:</label>
<div class="controls">
<form:select path="askUser" class="input-xlarge ">
<option value="">请选择</option>
<c:forEach items="${ask.wu}" var="list">
<option value="${list.wuId}">${list.wuName}</option>
</c:forEach>
</form:select>
</div>
</div>
<div class="control-group">
<label class="control-label">任务截止时间:</label>
<div class="controls">
<input name="askTime" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
value="<fmt:formatDate value="${ask.askTime}" pattern="yyyy-MM-dd HH:mm:ss"/>"
onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
</div>
</div>
<div class="control-group">
<label class="control-label">已检设施数:</label>
<div class="controls">
<form:input path="useNum" htmlEscape="false" maxlength="255" class="input-xlarge "/>
</div>
</div>
<div class="control-group">
<label class="control-label">未检设施数:</label>
<div class="controls">
<form:input path="unNum" htmlEscape="false" maxlength="255" class="input-xlarge "/>
</div>
</div>
<div class="control-group">
<label class="control-label">异常设施数:</label>
<div class="controls">
<form:input path="ycNum" htmlEscape="false" maxlength="255" class="input-xlarge "/>
</div>
</div>
<div class="control-group">
<label class="control-label">备注:</label>
<div class="controls">
<form:input path="remark" htmlEscape="false" maxlength="255" class="input-xlarge "/>
</div>
</div>
<div class="control-group">
<label class="control-label">任务状态:</label>
<div class="controls">
<form:select path="askStatus" class="input-xlarge required">
<form:option value="" label=""/>
<form:options items="${fns:getDictList('amp_ask')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
</form:select>
</div>
</div>
<div class="form-actions">
<shiro:hasPermission name="ask:ask:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
</div>
</form:form>
</body>
</html>

后台

/**
* Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
*/
package com.thinkgem.jeesite.modules.ask.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ask.entity.Ask;
import com.thinkgem.jeesite.modules.ask.entity.Eq;
import com.thinkgem.jeesite.modules.ask.entity.Wuask;
import com.thinkgem.jeesite.modules.ask.service.AskService;
import com.thinkgem.jeesite.modules.order.entity.Equiporder;
import com.thinkgem.jeesite.modules.order.service.EquiporderService;

/**
* 巡检任务成功Controller
* @author wu
* @version 2018-08-02
*/
@Controller
@RequestMapping(value = "${adminPath}/ask/ask")
public class AskController extends BaseController {

@Autowired
private AskService askService;
@Autowired
private EquiporderService equiporderService;

@ModelAttribute
public Ask get(@RequestParam(required=false) String id) {
Ask entity = null;
if (StringUtils.isNotBlank(id)){
entity = askService.get(id);
}
if (entity == null){
entity = new Ask();
}
return entity;
}
public static String[] ids=null;

@RequiresPermissions("ask:ask:view")
@RequestMapping(value = {"list", ""})
public String list(Ask ask, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<Ask> page = askService.findPage(new Page<Ask>(request, response), ask);
model.addAttribute("page", page);
return "modules/ask/askList";
}

@ResponseBody
@RequestMapping(value = "lists")
public List<Ask> lists(Ask ask) {
List<Ask> mapList =askService.lists(ask);
return mapList;
}

@RequiresPermissions("ask:ask:view")
@RequestMapping(value = "form")
public String form(Ask ask, Model model) {
// Equiporder equiporder=new Equiporder();
// equiporder.setWueqId("b9ed96d1b716491f8fc4f71f3705c50a");
// List<Equiporder> mapList =equiporderService.findUserList(equiporder);
// List<Wuask> areaMapList=new ArrayList<Wuask>();
// Wuask areaMap=new Wuask();
// for (Equiporder alarm2 : mapList) {
// areaMap.setWuId(alarm2.getWuId());
// areaMap.setWuName(alarm2.getWuName());
// areaMapList.add(areaMap);
// areaMap=new Wuask();
// }
// ask.setWu(areaMapList);
//---------------------------------------
// Ask ask1=new Ask();
// Eq e=new Eq();
// List<Eq> areaList=new ArrayList<Eq>();
// List<Ask> mapList1 =askService.lists(ask1);
// for (Ask alarm2 : mapList1) {
// e.setEqName(alarm2.getEqName());
// e.setEqId(alarm2.getEqId());
// areaList.add(e);
// e=new Eq();
// }
// ask.setEqc(areaList);
model.addAttribute("ask", ask);
return "modules/ask/askForm";
}
@RequiresPermissions("ask:ask:view")
@RequestMapping(value = "formselect")
public String formselect(Ask ask, Model model) {
Equiporder equiporder=new Equiporder();
equiporder.setWueqId("b9ed96d1b716491f8fc4f71f3705c50a");
List<Equiporder> mapList =equiporderService.findUserList(equiporder);
List<Wuask> areaMapList=new ArrayList<Wuask>();
Wuask areaMap=new Wuask();
for (Equiporder alarm2 : mapList) {
areaMap.setWuId(alarm2.getWuId());
areaMap.setWuName(alarm2.getWuName());
areaMapList.add(areaMap);
areaMap=new Wuask();
}
ask.setWu(areaMapList);
//---------------------------------------
// Ask ask1=new Ask();
Eq e=new Eq();
List<Eq> areaList=new ArrayList<Eq>();
List<Ask> mapList1 =askService.lists(ask);
for (Ask alarm2 : mapList1) {
e.setEqName(alarm2.getEqName());
e.setEqId(alarm2.getEqId());
areaList.add(e);
e=new Eq();
}
ask.setEqc(areaList);
model.addAttribute("askc", ask);
return "modules/ask/xzaskForm";
}
@ResponseBody
@RequestMapping(value = "check")
public static String[] check(HttpServletRequest request) {
ids=request.getParameterValues("check");
return ids;
}

@RequiresPermissions("ask:ask:edit")
@RequestMapping(value = "save")
public String save(Ask ask, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request){
if (!beanValidator(model, ask)){
return form(ask, model);
}
if(ids==null){
addMessage(redirectAttributes, "没有选择设备");
return "redirect:"+Global.getAdminPath()+"/ask/ask/?repage";
}
// for (int i = 0; i < ids.length; i++) {
// String s=ids[i];
// }
askService.save(ask,ids);
addMessage(redirectAttributes, "保存巡检任务成功成功");
return "redirect:"+Global.getAdminPath()+"/ask/ask/?repage";
}

@RequiresPermissions("ask:ask:edit")
@RequestMapping(value = "delete")
public String delete(Ask ask, RedirectAttributes redirectAttributes) {
askService.delete(ask);
addMessage(redirectAttributes, "删除巡检任务成功成功");
return "redirect:"+Global.getAdminPath()+"/ask/ask/?repage";
}

}

从数据库中取数据放入前台,checkbox里面的值(由数据库中来)

from表单中checkbox的多选,ajax转入后台,后台接受

from表单中checkbox的多选,ajax转入后台,后台接受