標籤: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區別