<script>
(($) => {
$(() => { //1:获取表单对象
const _form = $("form"); //const定义的是常量
//2:监听表单的提交事件
_form.on("submit",() => {
try {
//获取表单数据
const formData ={
tdUsername:_form.find("input[name='tdUsername']").val(),
tdPassword:_form.find("input[name='tdPassword']").val(),
tdCode:_form.find("input[name='tdCode']").val(),
token:_form.find("input[name='token']").val()
};
if (formData.tdUsername.length<3) {
new swal({
icon:"warning",
title:"FBI WARNING",
text:"请填写完整的登录账号"
});
return false;
}
if (formData.tdPassword.length<6) {
new swal({
icon:"warning",
title:"FBI WARNING",
text:"登录密码长度不能小于六位"
});
return false;
}
if (formData.tdCode.length!==6) {
new swal({
icon:"warning",
title:"FBI WARNING",
text:"请填写6位的验证码"
});
return false;
}
//通过ajax提交数据给接口
$.ajax({
url:_form.attr("action"),
data:JSON.stringify(formData),
type:"post",
dataType:"json",
async:true, //异步请求
cache:false, //不缓存请求结果
contentType:"application/json;charset=utf-8",
succsss(res){
if (res.code === 0) {
toastr.succsss(res.msg,res.title);
setTimeout(()=>{
window.location.href=res.url;
return;
},2000);
}else{
toastr.warning(res.msg,res.title);
}
},
error:function () {
toastr.error("由于未知原因请求失败,请稍后重试","请求失败");
}
});
}catch (e) {
console.error(e);
}
//3:阻止表单的默认提交行为
return false;
})
})
})(jQuery);
</script>