完成数据检查,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; } });
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211640.html原文链接:https://javaforall.net
