首页 > 代码库 > 普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别

普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别

示例:1.ajax版:

      $("#txtTitle").blur(function () {
                $.ajax({
                    type: "GET",
                    url: "Handler.ashx?NewsAdd=1&txtName=" + encodeURIComponent($(this).val()) + "",
                    data: null,
                    success: function (msg) {
                        if (msg != "") {
                            flag = false;
                            alert("不要重复添加!");
                        }
                        else
                            flag = true;
                    },
                    one rror: function () { alert("error") }
                });
            });

 

 

2.ajaxValidator版

           $("#txtName").formValidator({ onshow: "请填写物品名称", onfocus: "物品名称必须输入" }).inputValidator({ min: 1, max: 50, empty: { leftempty: false, rightempty: false, emptyerror: "不能有空格" }, one rror: "物品名称输入错误,可能长度超标" }).ajaxValidator({
                url: oldValue =http://www.mamicode.com/="" ? "Handler.ashx?GoodsAdd=1" : "Handler.ashx?GoodsEdit=1&oldValue="http://www.mamicode.com/+ oldValue +"",
                addidvalue: true,
                success: function (data) {
                    if (data =http://www.mamicode.com/="1")
                        return false;
                    else
                        return true;
                },
                buttons: $("#lbSubmit"),
                error: function () { alert("出错了"); },
                one rror: "请不要重复添加",
                onwait: "正在验证是否重复,请稍候..."
            }).defaultPassed();//修改时候加上

 

要注意的地方:

1.ajaxValidator传参时候通过$(id)得到的永远是空值。所以,用 addidvalue: true的解决方案。自动在url上追controlId=idVlaue;

2.$.ajax中onerror代表服务器程序错误状态,而ajaxValidator中代表,success状态下,return false的情况。ajaxValidator中代表服务器错误是error。

3.ajaxValidator修改时候,验证重复时候要加上defaultPassed(),否则一直验证不对

普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别