//校验提交的数据是否重复
/**
* url:后端的查询地址
* filedVal: 要传到后台的值
* ele:要绑定显示的元素,一般就是当前的input就可以,直接在其后边追加显示
* fn:回调函数,把查询到的值,回调给使用者
*
* json 后台传过来的数据{data:""}
**/
checkFiled: function (url, filedVal, ele, msg, fn) {
if (!msg) {
msg = "此项数据重复";
}
$.getJSON(url, {filed: filedVal}, function (json) {
if (json.data != null && json.data != 'null') {
$(ele).next().remove("span");
$(ele).parent().append('<span>' + msg + '</span>');
} else {
$(ele).next().remove("span");
} //写了函数
if (fn) {
//就执行这个函数
fn(json.data);
}
});
} //使用方式
var $input = $("input[name='customer.creditCode']");
$input.on('input', function (e) {
App.checkFiled("/YunApps/com_momathink_crm_zkhq/customer/checkCreditCode", $input.val(), $input, "社会信用代码重复", function (data) {
console.log("回调函数,拿到后台传过来的数据 " + data);
});
});

这个可以升级一下

App = {
    data:{},//编辑时候保存传过来的值,保存时候如果写的和传过来的值一样,那么就不是重复的,如果是添加那么这个值就是空,
  
//校验提交的数据是否重复
checkFiled: function (url,data,ele,msg,fn) {
if(!msg){
msg = "此项数据重复";
}
$.getJSON(url,data,function(json){
if(json.data != null && json.data != 'null'){
$(ele).next().remove("span");
$(ele).parent().append('<span style="color:#ff0000;">' + msg + '</span>');
}else{
$(ele).next().remove("span");
} //写了函数
if(fn){
fn(json.data);
}
});
},
/**
* 检测客户的这个数据是否重复
* @param appVal app保留下的值,这个值在编辑的时候是作为全局变量存下的
* @param ele 绑定事件的元素,一般是input框
* @param filed 查询的数据库字段
* @param msg 提示的消息
*/
checkCustomerFiled:function(appVal,ele,filed,msg){
var $input = $(ele);
$input.on('input',function(e){
//编辑和原来的值一样不检测
if(appVal == $input.val()){
return ;
}
App.checkFiled("/YunApps/com_momathink_crm_zkhq/customer/checkField",{'filed':filed,'val':$input.val()},$input,msg);
});
},
//监测重复值,使用方法
checkFieldMethod:function(){
console.log("开始添加检测重复")
var $input = $("input[name='customer.creditCode']");//要检测的input元素         /*
        * App.data.id 编辑时候保存全局的id ,放置我写了一个不想要的值之后,不想改了,回到原来的值后保重复的问题,如果是添加这个值就是空,那就没问题了
        * $input 要校验的元素,这个元素的值就是下边这个字段传到后台的值
        * creditCode 传到后台的要校验的数据库中的这个字段的名字
        * 最后的文字 就是我们重复后提示的文字
        */
App.checkCustomerFiled(App.data.id,$input,'creditCode','社会信用代码重复')
var $inputcompanyName = $("input[name='customer.companyName']");
App.checkCustomerFiled(App.data.companyName,$inputcompanyName,'companyName','公司名称重复')
},
}
 

后台方法,改进后的

 /**
* 根据字段和值查询数据
* @param filed 数据库表字段
* @param val 字段对的值
* @return
*/
public static Integer getIdByFiledAndVal(String filed,String val){
if(null == filed || null == val){
return null;
}
CrmCustomer crmCustomers = dao.findFirst("SELECT id FROM crm_customer WHERE " + filed + "=? limit 1", val);
if(null == crmCustomers){
return null;
}
return crmCustomers.getId();
} 传到前台的是一个json
{data,""}

如果再次改动,可以把数据库的名字加上,这样就可以对不同的表进行查询了

js回调函数,检测这个值是否重复的更多相关文章

  1. JS回调函数(callback)

    在使用Jquery的时候,用到Callback(),回调函数的概念.而且很多. 比如: $.ajax({ url:"test.json", type: "GET" ...

  2. C# dll 事件执行 js 回调函数

      C# dll 事件执行 js 回调函数   前言: 由于js 远程请求  XMLHttpRequest() 不支持多线程,所以用C# 写了个dll 多线程远程抓住供js调用. 最初代码为: C#代 ...

  3. js 回调函数小例子

    js 回调函数小例子 <script> //将函数作为另一个函数的参数 function test1(){ alert("我是test1"); } function t ...

  4. js回调函数的理解

    js回调函数(callback)理解 Mark! 讲之前说一句 function say(){ alert(,,,,,,,,) } var say=function (){ alert(,,,,,,, ...

  5. JS回调函数全解析教程

    转自:http://blog.csdn.net/lulei9876/article/details/8494337 自学jQuery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速g ...

  6. 学习js回调函数

    <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title> ...

  7. 如何理解JS回调函数

    1.回调函数英文解释: A callback is a function that is passed as an argument to another function and is execut ...

  8. js回调函数(callback)理解

    Mark! js学习 不喜欢js,但是喜欢jquery,不解释. 自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函 ...

  9. js回调函数

    自学jQuery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...

随机推荐

  1. djangobb之forum.topics

    看djangobb源代码时,看到view的show_forum(request, forum_id, full=True)时,对于topics = forum.topics.order_by('-st ...

  2. oracle中查询表是否存在

    select count(*) from user_tables where table_name='表名' 或者 select 1 from user_tables where table_name ...

  3. Django 数据库的迁移

    先数据库迁移的两大命令: python manage.py makemigrations & python manage.py migrate 前者是将model层转为迁移文件migratio ...

  4. Oracle 学习总结 - 内存优化

    实例内存优化 开启自动内存管理 1. 设置memory_max_target alter system set memory_max_target=1G scope=spfile; /*init.or ...

  5. 1047A_Little C Loves 3 I(构造)

    A. Little C Loves 3 I time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. js Json数组的增删改查

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  7. 手工获取AWR报告

    AWR(Automatic Workload Repository)报告常用于Oracle数据库性能分析.熟练解读AWR报告有助于快速分析Oracle性能问题.下面主要描述如何手工获取AWR报告. 操 ...

  8. python实现排序算法三:插入排序

    插入排序基本思想:假设一个无序数组A,则对于只有一个元素A[0]的子数组C来讲,其是有序的,然后将A[1]插入到C中,则就是将A[1]与A[0]进行比较,如果A[1]比A[0]小,则交换两者的顺序,这 ...

  9. 为什么使用DLL

    (1) 如果不同的程序使用相同的 DLL,只需将 DLL 在内存中装载一次,这样节省了系统内存.DLL 映射到每个进程(每运行一次应用程序)的专用地址空间中,但它们的代码使用的内存影像程序只在内存中装 ...

  10. Canvas中 drawImage绘制图片不显示

    在学习 html5中的 Canvas.drawImage时写了如下代码: <!doctype html> <html> <head><title>研究& ...