当一个注册表单的提交按钮点击后,通过绑定的时间来确定用户名、邮箱等是否有效,最后来确定是否提交表单。先设置一个变量status值为true,然后通过ajax来判断用户名、邮箱等是否已经注册,如果已注册就在回调函数中设置status的值为false,最后再判断status的值是true还是false来决定是否提交。判断status的代码写在ajax的后面,但是还是先执行了判断status的代码,再执行了ajax回调函数中设置status的代码,不知道该怎么做?window.cb={};cb.register={};$('#register-submit').click(function(){var$self=$(this);var$form=$(this).closest('#register-form');var$username=$form.find('#register-username');varusername=$username.val();cb.register.status=true;if(!username.match(/^[0-9a-z_\u4e00-\u9af5]{2,14}$/i)){$username.next('span').remove();$username.after('无效的用户名'); cb.register.status=false;}else{$username.next('span').remove();$.post('/auth/ajax',{username:username},function(data){if(data.status==false){$username.after('用户名已注册'); cb.register.status=false;}},'json');}var$email=$form.find('#register-email');varemail=$email.val();if(!email.match(/^[0-9a-z_\-]+@[0-9a-z_\-]+\.[a-z]{2,4}$/i)){$email.next('span').remove();$email.after('无效的邮箱'); cb.register.status=false;}else{$email.next('span').remove();$.post('/auth/ajax',{email:email},function(data){if(data.status==false){$email.after('邮箱已注册'); cb.register.status=false;console.log(cb.register.status);}},'json');}var$pwd=$form.find('#register-password');varpwd=$pwd.val();if(!pwd.match(/^[0-9a-z_\-#@$*%&]{6,16}$/i)){$pwd.next('span').remove();$pwd.after('无效的密码'); cb.register.status=false;}console.log('hello');console.log(cb.register.status);returnfalse;if(cb.register.status==false){returnfalse;}$.post('/auth/register',{email:email,name:username,password:pwd},function(data){if(data.status==true){$self.css('display','none');var$message=$('#message');vars1='注册成功'; vars2='一封邮件已发送到邮箱,请在1小时内验证(过期后需重新发送验证邮件)';$message.find('.message-body').append(s1+s1);}else{var$token=$form.find('#token');$token.attr('name',data.token.name);$token.attr('value',data.token.value);$form.find('.submit-tips').remove();$form.prepend('注册失败,请重新提交
}},'json');returnfalse;});
温温酱
回首忆惘然
相关分类