普通Jquery的ajax判斷重複和formvalidator的ajaxValidator區別

來源:互聯網
上載者:User

標籤:io   strong   for   sp   cti   on   c   amp   ad   

樣本: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;
                    },
                    onerror: function () { alert("error") }
                });
            });

 

 

2.ajaxValidator版

           $("#txtName").formValidator({ onshow: "請填寫物品名稱", onfocus: "物品名稱必須輸入" }).inputValidator({ min: 1, max: 50, empty: { leftempty: false, rightempty: false, emptyerror: "不能有空格" }, onerror: "物品名稱輸入錯誤,可能長度超標" }).ajaxValidator({
                url: oldValue == "" ? "Handler.ashx?GoodsAdd=1" : "Handler.ashx?GoodsEdit=1&oldValue=" + oldValue + "",
                addidvalue: true,
                success: function (data) {
                    if (data == "1")
                        return false;
                    else
                        return true;
                },
                buttons: $("#lbSubmit"),
                error: function () { alert("出錯了"); },
                onerror: "請不要重複添加",
                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區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.