用于校验字段是否重复

//后台代码
	/**
	 * 用于校验字段是否重复
	 * 
	 * @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;
			  }
			}
		});
	});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值