function checkCode(){ var temp; $.ajax({ cache:false, async:false, type:"POST", url:"doJSP/doCode.jsp", data:"rand="+code.val(), success:function(rand){ if(parseInt(code.val())==parseInt(rand) && code.val()!=""){ right(code,codeError); codeError.html(" 验证码输入正确."); temp=true; }else{ temp2="false"; wrong(code,codeError); codeError.html(" 验证码输入错误,可以点击图片刷新."); temp=false; } } }); return temp;
关键这个属性: async:false
默认为true,表示异步请求,改为false同步请求,就能取到上面代码temp的值...困扰蛮久的问题,开始还以为是js的作用域,测试无果,终于找到原来是$.ajax没设置好- -!