您的位置:首页 > 其它

ajaxSubmit异步提交

2016-05-18 14:12 323 查看
完成数据检查,form数据拼装,ajax异步提交数据,提交不刷新页面。

jQuery(document).ready(function() {

/*
Fullscreen background
*/
$.backstretch("images/login_backgroud.jpg");

/*
Form validation
*/
$('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea').on('focus', function() {
$(this).removeClass('input-error');
});

$('.login-form').on('submit', function(e) {
$(this).find('input[type="text"], input[type="password"], textarea').each(function(){
if( $(this).val() == "" ) {
e.preventDefault();
$(this).addClass('input-error');
}
else {
$(this).removeClass('input-error');
}
});
toSubmit(this);
return false;
});

function toSubmit(frm){
var obj = getFormJson(frm);
$(this).ajaxSubmit({
data:obj,
type:"post", //提交方式
dataType:"json", //数据类型
url:"billingLogin", //请求url
clearForm:true,
resetForm: true,
success:function(data){ //提交成功的回调函数
if(data.status == 1){
location.href = "/getPackages";
}else{
swal("OMG!", "登录信息有误!", "error");
}
}
});
}

//将form中的值转换为键值对。
function getFormJson(frm) {
var o = {};
var a = $(frm).serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});

return o;
}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajaxSubmit