<script>
//显示添加分类块
function showAddDailyType(){
document.getElementById("divDddDailyType").style.display="";
//document.getElementById("more").style.display="block";
}
//ajax 进行添加操作
function addDailyType(){
var dailyTypeName = document.getElementById("dailyTypeName").value;
$.getJSON("${pageContext.request.contextPath}/cultivatespace/dailyManager.do?method=addDailyType&dailyTypeName="+dailyTypeName+"&userId=${userId}&dailyTypeListID=${dailyTypeListID}&dailyListSort=${dailyListSort}", function(json){
var cnt=json.length;
var html="";
var dailyTypeId = json.dailyTypeId;
var dailyTypeName = json.dailyTypeName;
//js添加select选项
document.getElementById("selectDailyType").options.add(new Option(dailyTypeName ,dailyTypeId ));
});
document.getElementById("divDddDailyType").style.display="none";
var dailyTypeName = document.getElementById("dailyTypeName").value="";
}
</script>
<!-- 日志类型 -->
<select>
<c:forEach var="dailyType" items="${dailyTypeList}"
varStatus="row">
<c:if test="${(!empty dailyDto.dailyTypeId) && (dailyDto.dailyTypeId ==dailyType.dailyTypeId)} ">
<option value="${dailyType.dailyTypeId}" selected="selected">
${dailyType.dailyTypeName}
</option>
</c:if>
<c:if test="${empty dailyDto.dailyTypeId}">
<option value="${dailyType.dailyTypeId}" >
${dailyType.dailyTypeName}
</option>
</c:if>
</c:forEach>
</select>
<input value="添加分类" />
<div style="display: none;">
分类名称:
<input />
<input value="确定" />
<input value="取消" />
</div>
java类
public ActionForward addDailyType(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws SaveException, QueryException, IOException {
String userId = request.getParameter("userId");
// System.out.println(userId);
String dailyTypeName = request.getParameter("dailyTypeName");
TKgGrDailyType dailyType = new TKgGrDailyType();
dailyType.setDailyTypeCreatetime(CommonUtil.formatDate(new Date()));
dailyType.setDailyTypeName(dailyTypeName);
dailyType.setDailyUserId(userId);
dailyManagerBo.addDailyType(dailyType);
String json = JsonUtil.bean2json(dailyType);
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
return null;
}
//JsonUtil类
public static String bean2json(Object bean) {
StringBuilder json = new StringBuilder();
json.append("{");
PropertyDescriptor[] props = null;
try {
props = Introspector.getBeanInfo(bean.getClass(), Object.class)
.getPropertyDescriptors();
} catch (IntrospectionException e) {
}
if (props != null) {
for (int i = 0; i < props.length; i++) {
try {
String name = object2json(props[i].getName());
String value = object2json(props[i].getReadMethod().invoke(
bean));
json.append(name);
json.append(":");
json.append(value);
json.append(",");
} catch (Exception e) {
}
}
json.setCharAt(json.length() - 1, '}');
} else {
json.append("}");
}
return json.toString();
}