Re: [ js ] 有關 function return string的問題

看板Ajax作者 (!!M)時間14年前 (2010/05/09 17:20), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/3 (看更多)
試試看這樣子 function accountCheck(a,callback) { // .... // $.post('fetch.php', /* ... */ , function(data) { callback(data); }); } $('#register_submit').click(function(){ var acc = $('#register_account').val(); accountCheck( acc, function(result) { alert(result); }); }); ※ 引述《ot32em (reverse(wasefo);)》之銘言: : $(document).ready(function(){ : ... : function accountCheck(a){ : if( a.length < 3 ) return 'short'; : if( a.length > 11) return 'long'; : $.post('fetch.php', { "type":"ncuaccount", "email": 'someemail' : , "week": '30'} : , function(data){ alert(data);return data; } ); : } : 綠色的可以回傳資料 : 可是在下面我alert result時 : 卻是undefined or 空字串 : 這會是因為.post要等 然後 function accoutnCheck(a) 等不及就return的關係嗎? : $('#register_submit').click(function(){ : var acc = $('#register_account').val(); : var result = ""; : result = accountCheck( acc ); : alert(result); : ... : }); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 115.64.54.125

05/09 17:24, , 1F
感謝解答 不過我已經改成把code都放進.POST裡的function裡
05/09 17:24, 1F

05/09 17:25, , 2F
callback的機制真的很酷炫XD 只是我還沒上手 我來TRYTRY
05/09 17:25, 2F
文章代碼(AID): #1Bvdv2Tw (Ajax)
文章代碼(AID): #1Bvdv2Tw (Ajax)