作者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
1F:推 ot32em:感谢解答 不过我已经改成把code都放进.POST里的function里 05/09 17:24
2F:→ ot32em:callback的机制真的很酷炫XD 只是我还没上手 我来TRYTRY 05/09 17:25