weixin_33725807 2010-05-25 12:23 采纳率: 0%
浏览 25

jQuery中的变量问题

I don't understand why I'm not getting the message as "username already exits" if type the username which is already in database.

If the username in a server, then it is returning the value as "1" otherwise empty, and despite of successfully getting the value from the server based on username is present or not, and assigning the value to variable "x", I'm unable to get message when I pass already exist username. May I know?

$(document).ready(pageInit);
function pageInit() {
$('#1').bind('blur',go);
}
function go() {
 var value = $('#1').val();
 var x = 0;
 $.post('just.do',{username:value},function(data){
    x = data;

   }
);

 if(x) {
      $('#para').show();
      $('#para').html("Username already exits");
         return false;
  }
 else {
    $('#para').hide();
       return true;
  } 
};

EDIT: This is what I'm doing in post request in servlets:

String user1 = request.getParameter("username");  
   if(user != null) {
         String query = "Select * from users where user_name="+"\""+user+"\"";
         b = db.doExecuteQuery(stmt,query);
         if(b) {
          out.write("1");
         }

}

  • 写回答

2条回答 默认 最新

  • weixin_33675507 2010-05-25 12:26
    关注

    Your code if(x) ... executes before the http request finishes, and as a result, x will never be set when that part of the code is reached.

    You want to use callbacks:

    $.post('just.do',{username:value},function(data) {
       receivedResult(data);
    });
    
    ...
    
    function receivedResult(x) {
        if(x) {
         ...
        } else {
         ... 
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题