问题描述
- Ajax使用-当选中职位类别时,它所对应的父岗位ID就会用复选框方式显示出来
-
控制器内:public ModelAndView addPosition() { Map<String, Object> map = new HashMap<String, Object>(); return new ModelAndView("admin/positionAdd",map); } public String savePosition(HttpServletRequest request) throws IOException { String post_type = request.getParameter("post_type"); String post_code = request.getParameter("post_code"); String post_name = request.getParameter("post_name"); String post_duties = request.getParameter("post_duties"); String parent_id = request.getParameter("parent_id"); String check_state = request.getParameter("check_state"); String check_note = request.getParameter("check_note"); String state = request.getParameter("state"); Position position = new Position(); position.setId(Common.returnUUID()); position.setPost_type(post_type); position.setPost_code(post_code); position.setPost_name(post_name); position.setPost_duties(post_duties); position.setParent_id(parent_id); position.setCheck_state(check_state); position.setCheck_note(check_note); position.setState(state); positionService.insert(position); return "redirect:positionList.do"; } @RequestMapping("admin/editPosition.do") public ModelAndView editPosition(ModelMap modelMap, String id) { Position position = (Position) positionService.selectByID(id); modelMap.put("p", position); return new ModelAndView("admin/positionEdit", modelMap); } public String editPosition(@ModelAttribute("Position") Position position) throws IOException { positionService.update(position); return "redirect:positionList.do"; } public String deletePosition(String id) { Position position = new Position(); position.setId(id); positionService.delete(position); return "redirect:positionList.do"; } public String positionList(HttpSession session, HttpServletRequest request,HttpServletResponse response) { String post_type = request.getParameter("post_type"); @SuppressWarnings("unchecked") List<Position> pList = this.positionService.selectList(post_type); StringBuffer sb= new StringBuffer(); sb.append("<tr id = 'select'>"); for (Position position : pList) { sb.append("<td> <input type='checkbox' id='position' name='position' value ='"+position.getPost_code()+"'>" +position.getPost_name()+ "</td>"); } sb.append("</tr>"); try { response.getWriter().println(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
JSP页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">添加企业岗位
function changeCK() {var selectedValue = document.getElementById("post_type");
var selectedIndex = selectedValue.selectedIndex;
var ajaxUrl = "duiying.do?post_type="
+ selectedValue.options[selectedIndex].value;
ajaxFunction(ajaxUrl);
document.getElementById("scope").style.display = "";}
function getScope() {}
function setVals() {
var id = document.getElementsByName("position");
var value = "";
var name = "";
for(var i = 0; i < id.length; i++){
if(id[i].checked)
value += id[i].value+",";
name += id[i].text+",";
}//alert(value);
document.getElementById("parent_id").value = value;//"111,222";
document.getElementById("temp1").value = name;//"111,222";
document.getElementById("scope").style.display = "none";
}function ajaxFunction(url) {
//考虑到不同浏览器的兼容性,所以做出判断。var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveObject)
{
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {
}
}
}
//监控和接受后台传的字符串
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var result = xmlHttp.responseText;//document.getElementById("select").html();
//alert(result);
$("#select").html(result);
//select
fenge(result);
//"<p><input type='checkbox' id='checkbox' name='${ts.practice_code}' value='${ts.stu_code}' > ${ts.stu_name}+ ${ts.practice_code}</p>";
}
};xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}//分割解析字符串。
function fenge(neirong) {}
<form name="form1" id="myform" method="post" action="doAddPosition.do"> <table border="0" width="400"> <tr> <td width="100">职位类别:</td> <td width="300"><select name="post_type" id="post_type" onChange="changeCK()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select></td> </tr> <tr> <td width="100">岗位编码:</td> <td width="300"><input type="text" name="post_code" /></td> </tr> <tr> <td width="100">岗位名称:</td> <td width="300"><input type="text" name="post_name" /></td> </tr> <tr> <td width="100">岗位职责:</td> <td width="300"><input type="text" name="post_duties" /></td> </tr> <tr> <td width="100">父岗位id:</td> <td width="300"><input type="text" name="parent_id" /></td> </tr> <tr> <td width="100">审核备注:</td> <td width="300"><input type="text" name="check_note" /></td> <tr> <td width="100">状态:</td> <td width="300"><select name="state" id="state"> <option value="1">有效</option> <option value="2">无效</option> </select> </td> </tr> </table> <div id="scope" style="display:none"> <table> <tr id = "select"><td>请选择</td></tr> </table> <button type="button" onClick="setVals()">确定</button> </div> <div style="margin-top:20px;"> <input type="submit" value="保存" /> <button type="button" onclick="window.location='./positionList.do'">返回</button> </div> </form>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">修改企业岗位
function changeBoxes(action) {
var oForm = document.forms["myForm1"];
var oCheckBox = oForm.college;
for(var i=0;i<oCheckBox.length;i++) //遍历每一个选项
if(action<0) //反选
oCheckBox[i].checked = !oCheckBox[i].checked;
else //action为1是则全选,为0时则全不选
oCheckBox[i].checked = action;
}<form name="form1" method="post" action="doEditPosition.do"> <input type="hidden" name="id" name="id" value="${p.id}"> <table border="1" width="400"> <tr> <td width="100">职位类别:</td> <td width="300"><input type="text" name="post_type" value="${p.post_type}"> </td> </tr> <tr> <td width="100">岗位编码:</td> <td width="300"><input type="text" name="post_code" value="${p.post_code}"> </td> </tr> <tr> <td width="100">岗位名称:</td> <td width="300"><input type="text" name="post_name" value="${p.post_name}"> </td> </tr> <tr> <td width="100">岗位职责:</td> <td width="300"><input type="text" name="post_duties" value="${p.post_duties}" /> </td> </tr> <tr> <td width="100">父岗位id:</td> <td width="300"><input type="text" name="parent_id" value="${p.parent_id}" /> </td> </tr> <tr> <td width="100">审核备注:</td> <td width="300"><input type="text" name="check_note" value="${p.check_note}" /> </td> </tr> <tr> <td width="100">状态:</td> <td width="300"><input type="text" name="state" value="${p.state}" /> </td> </tr> </table> <div style="margin-top:20px;"> <input type="submit" value="保存" /> <button type="button" onclick="window.location='./positionList.do'">返回</button> </div> </form>