我的 Ajax 调用在我的函数内成功运行,但我无法在函数外返回结果。这与 Ajax 或我如何尝试从函数返回结果有关吗?
HTML
<table>
<tr>
<th> Vote </th>
</tr>
<tbody>
<tr class="vote">
<td id="upvote">1</td>
<td id="downvote">-1</td>
</tr>
<tr>
<td id="newvote"></td>
</tr>
</tbody>
</table>
jQuery
$(document).ready(function(e) {
var myvote = allFunction4(); //returns undefined
alert(myvote);
})
function allFunction4() {
$('.vote').children().click(function() {
var vote = $(this).text();
var timestamp = 1369705456; //value I know exits in db
$.post('forumvote.php', {'timestamp': timestamp, 'vote': vote}, function(result, success) {
var newvotes = result;
alert(newvotes); //this works
return newvotes;
})
})
}