ajax提交并根据返回的值重写标签

Jsp:

function saveCalendar(id){
		var startTmDetail = $("#startTimeDetail"+id).val();
		var startHour = $("#startHour"+id).val();
		var endTmDetail = $("#endTimeDetail"+id).val();
		var endHour = $("#endHour"+id).val();
		var content = $("#contentDetail"+id).val();
		var param = [
						{name:'startTmDetail', value:startTmDetail},
						{name:'startHour', value:startHour},
						{name:'endTmDetail', value:endTmDetail},
						{name:'endHour', value:endHour},
						{name:'content', value:content},
						{name:'id', value:id}
					];
		$.ajax({
			type: 'post',
			url: '<%=path %>/calendarInfo/updateCalDetail.action',
			async: false,//想要执行完这个ajax再执行下边代码,就要设置成false
			data: param,
			dataType: 'text',
			success: function(text){
				var jsonObj = eval("("+text+")"); 
				var divStr = "";
				var divContent = "";
				//重写divStr
				divStr = "<div id='showTm"+jsonObj.id+"'>"+jsonObj.startTime+" 到 "+jsonObj.endTime+"&nbsp;&nbsp;<input style='border: 1px solid #777;' type='button' value='编辑' onclick='calendarEdit("+jsonObj.id+")' /></div>";
				divContent = "<div id='showContent"+jsonObj.id+"'>"+jsonObj.content+"</div>";
				$("#showTm"+id).html(divStr);
				$("#showContent"+id).html(divContent);

				$("#showTm"+id).show();
				$("#hidTm"+id).hide();
				$("#showContent"+id).show();
				$("#hidContent"+id).hide();
			},
			error: function(text) {
				alert('失败');
			}
		});
	}

Action:

@RequestMapping("/calendarInfo/updateCalDetail")
	public void updateCalDetail(HttpServletResponse res,HttpServletRequest request) throws Exception{
		res.reset();
		res.setContentType("text/html;charset=utf-8");
		PrintWriter pw = res.getWriter();
		
		String id = request.getParameter("id");
		String startTm = request.getParameter("startTmDetail");
		String startHour = request.getParameter("startHour");
		String endTm = request.getParameter("endTmDetail");
		String endHour = request.getParameter("endHour");
		String content = request.getParameter("content");
		
		String startStr = startTm + " " +startHour + ":00:00";
		String endStr = endTm + " " +endHour + ":00:00";
		CalendarDetail calDetail = new CalendarDetail();
		calDetail = calDetailService.findCalendarDetailById(Long.parseLong(id));
		calDetail.setStartTime(startStr);
		calDetail.setEndTime(endStr);
		calDetail.setContent(content);
		
		calDetailService.update(calDetail);
		
		JSONObject jsonObject = JSONObject.fromObject(calDetail);
		System.out.println(jsonObject); 

		pw.print(jsonObject);
		pw.flush();
		pw.close();

	}

相关推荐