作者evernever (!!M)
看板Ajax
標題Re: [ js ] 有關 function return string的問題
時間Sun May 9 17:19:59 2010
試試看這樣子
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
推 ot32em:感謝解答 不過我已經改成把code都放進.POST裡的function裡 05/09 17:24
→ ot32em:callback的機制真的很酷炫XD 只是我還沒上手 我來TRYTRY 05/09 17:25