看板 Ajax 關於我們 聯絡資訊
我在模擬一個登入時用ajax檢查帳號是否存在的程式 $(document).ready(function(){ $("form[name='form']").submit(function (){ //*1主函式 acc = $("input[name='account']").val(); pwd = $("input[name='passwd']").val(); if (acc=="" || pwd==""){ alert('account or password empty'); return false;} else return check_account(acc,pwd); //*2這邊呼叫函數用ajax檢查帳號 return false; }); }); function check_account(acc,pwd){ $.post("check_acc.php",{account:acc,passwd:pwd},function(response){ if (response == 1){ alert('account exist'); return true;} //*3 else { alert('account not exist'); return false;} //*4 }); } 結果*3和*4 的return 無法直接傳回給*2 而且在*1函式跑完之後才會跳出alert訊息 不知道能不能直接取得 $.post的結果 像是 var return_value = $.post.. 再判斷return_value去跑? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.104.202.90
chph:$.ajax({ 中略 , success:function(res){成功時的處理} }); 12/19 22:35
TonyQ:他的$.post(param,function(response){}) 跟你的函式等義. 12/19 23:25
neversay:我建議是在onblur時就檢查,on submit會變成各自分開跑 12/19 23:39
appleboy46:http://blog.wu-boy.com/2008/09/22/412/ 參考一下 12/20 14:17