一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JQuery中ajax异步验证表单简单实例

时间:2013-10-31 编辑:简简单单 来源:一聚教程网

$.get和$.post方法一样,第一个参数是地址,第二个是传的值,第三个是成功后的回调函数
post方法:

 代码如下 复制代码

function emailb(){
var email = $("#email").val();
if(email==""){
$("#inf_email").replaceWith('
Email地址不能为空
');
}
else{
$.post("/index.php/Index/checkEmail2",{"email":email,"randnum":Math.random()+''},function

(res){
if(res=='1'){
$("#inf_email").replaceWith('
可以使用
');
}
else{
$("#inf_email").replaceWith('
'+res+'
');
}
});
}
}

get方法:

 代码如下 复制代码

var p = new prompts();
$('#username').blur(function(){
var username = $('#username').val();    //获取表单值
if(username == ''){
p.p('#user_prompt','p1','p2 p3','大小写英文字母、汉字、数字、下划线组成的长度 3-12 个字节以

内');                c_user = '';
}else if(username.length < 3){
p.p('#user_prompt','p2','p1 p3','用户名长度错误!');
c_user = '';
}else if(!validate.username(username)){
p.p('#user_prompt','p2','p1 p3','此用户名不合法。');
c_user = '';
}else{
//判断用户名是否存在 ajax 基于jQuery
$.get("chk_username.php",{username:$('#username').val()
},function(data,textStatus){
if(data.indexOf('true') != -1){
p.p('#user_prompt','p3','p1 p2','恭喜您,您可以使用这个用户名注册!');
c_user = 'yes';
}else{
p.p('#user_prompt','p2','p1 p3','该用户名已经被注册,请选用其他用户名。');
c_user = '';
}
})
}
chkreg();//每一次鼠标离开都验证一下。
function chkreg(){
if(c_user == 'yes' && c_pwd1 == 'yes' && c_pwd2 == 'yes' && c_email == 'yes' && c_checkcode

== 'yes'){
flag = true;
}else{ flag = false;}
}

ajax方法


     
     
   
     
     
   
     
     
     
         
         
         
     

$('#buttonajax').click(function(){
 //alert($(this).val());
 //checkem();
 var postData = $('#submitform').serialize();
 //alert(postData);
 $.ajax({
  type: "POST",
  url: "/xxxx.php",
  data: postData,
  success: function(msg){
   //alert(msg);
   if(msg==1){
    alert('您的问题已提交成功!');
    $('#bak1').val('');
    $('#mo').val('');
   
   }else if(msg==-1){
    alert('您的问题提交失败,请认真填写提交内容!');
   
   }else if(msg==-2){
    alert('请输入正确手机号码!');
   }
   else if(msg==-3){
    alert('对不起,你提交的内容中有非法字符!');
   }
   else if( msg== -4 )
   {
    alert('对不起,您己提交此问题,请不要重复提交!');
   }
   else
   {
    alert('对不起,系统忙请稍后再试!');
   }
  }
 });
})

 代码如下 复制代码


       
         
         
         
         
         
       
     
手机:

/>

邮箱:

/>

留下您的手机号码,有最新消息我们会发送到您手机


        <textarea name="bak" cols="80" rows="5" class="lab" id="bak1">

/>
                提出疑问,将有专业的置业顾问或房乐网网友帮您解答

热门栏目