JQuery ajax回调函数

转自http://zhengyh2008.blog.163.com/blog/static/13894713201003041633526/

jquery ajax 回调函数 返回值  

js文件:

$.ajaxSetup({
  async: false
  });

function oncheck(oid){
    var rs=true;
        
    if(oid=="stunum") {                
        $.post("util.php",checkString(oid),function(data){
      if(decodeURI(data).trim()== 'used'){
            
            alert("该学号已换注册!");
           
             rs=false; 
            }
           
        });
    }
  
   return rs;
}
function onSub(){

    return oncheck("stunum");

    
}

 

注意: 函数oncheck(oid){的写法,写法2.的返回值可能永远是true;

写法1:function oncheck(oid){
    var rs=true;
        
    if(oid=="stunum") {                
        $.post("util.php",checkString(oid),function(data){
      if(decodeURI(data).trim()== 'used'){
            
            alert("该学号已换注册!");
           
             rs=false; 
            }
           
        });
    }
  
   return rs;
}
写法2 写法1:function oncheck(oid){
   
        
    if(oid=="stunum") {                
        $.post("util.php",checkString(oid),function(data){
      if(decodeURI(data).trim()== 'used'){
            
            alert("该学号已换注册!"); 
            
             retrun false; 
            }
           
        });
    }
  
   return true;
}

Ajax请求默认的都是异步的
如果想同步 async设置为false就可以(默认是true)

var html = $.ajax({
  url: "some.php",
  async: false
}).responseText;

或者在全局设置Ajax属性

posted @ 2011-12-19 22:32  黎明露珠  阅读(1609)  评论(0编辑  收藏  举报