//后台代码
/**
* 用于校验字段是否重复
*
* @param request
* @return
*/
@RequestMapping(params = "checkField")
@ResponseBody
public AjaxJson checkField(HttpServletRequest request, String tableName,
String fieldName, String value, String id) {
AjaxJson aj = new AjaxJson();
Map mp = new HashMap();
if (StringUtils.isNotEmpty(tableName)
&& StringUtils.isNotEmpty(fieldName)
&& StringUtils.isNotEmpty(value)) {
String sql = "select * from " + tableName + " where " + fieldName
+ "= '" + value + "'";
System.out.println(sql);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list = jdbcTemplate.queryForList(sql);
int n = list.size();
if (id != null || !"".equals(id)) {
for (Map<String, Object> a : list) {
if (a.get("id").toString().equals(id))
n--;
}
}
mp.put("msg", n);// 重复
aj.setAttributes(mp);
} else {
mp.put("msg", 100);// 字段数据不全
aj.setAttributes(mp);
}
aj.setSuccess(true);
return aj;
}
//前端js
//验证房屋编号重复
$(function(){
parent.$(".ui_state_highlight").click(function(){
var code= $("#house_code").val();
var id= $("input[name='id']").val();
var a="";
$.ajax({
async : false,
cache : false,
type : 'POST',
contentType : 'application/json',
dataType:"json",
url : "cgController.do?checkField&tableName=ais_house&fieldName=house_code&value="+code+"&id="+id ,
error : function() {
alert('出错了');
frameElement.api.close();
},
success : function(data) {
a=data.attributes.msg;
}
});
if(a==0){
return true;
}else {
if(a==100){
alert("请输入编号");
return false;
}else{
alert("编号重复,请重新输入");
return false;
}
}
});
});