HTML5表单验证与AjaxFrom结合用法
在某些特定时候,不知道什么的情况下,出现了HTML5原生的验证,使用ajaxForm提交后无效的情况。
所以自己写了这个小函数,用来解决该问题,只需要将该函数加入到ajaxForm的beforeSubmit中即可;
ep:
var _form, _btn, _text, _chk;
var submit_option = {
type: "post",
dataType: "json",
beforeSubmit: function() {
return checkForm(_form);
xcwl.loading();
_btn.attr("disabled", "disabled").text("正在提交...");
},
success: function(data) {
//成功后返回
if (data.success == true) {
xcwl.tips(data.message, function() {
xcwl.go("{U(array(m=>member))}")
})
_form.resetForm();
} else {
xcwl.tips(data.message);
}
xcwl.loading(1);
_btn.removeAttr("disabled").text(_text);
}
}下面就是核心函数啦。
window.checkForm = function(theform) {
var myform = (theform instanceof jQuery) ? _form[0] : theform;
myform = typeof(myform) == "object" ? myform : document.querySelector(theform);
if (myform == undefined) {
return false;
}
for (var i = 0; i < myform.elements.length; i++) {
var el = myform.elements[i],
isvalid = el.checkValidity();
if (!isvalid) {
var tipinfo = el.getAttribute("title") ? el.getAttribute("title") : el.getAttribute("placeholder");
if (tipinfo) {
alert(tipinfo);
el.focus();
}
return false;
}
}
return true;
}
