我通過這個很多帖子去了,但沒有找到任何解決方案爲我工作 - 清理代碼:廣東話訪問.post的可變
$('#form-new').submit(function(e){
e.preventDefault();
$curForm = $(this);
$textbox = $('.new-box' ,this);
window.tmp_input_ok = false;
var wasError = false;
if($.trim($textbox.val()) == ""){
wasError = true;
}
if(wasError){
//possible message
} else{
var jqxhr = $.post(
$(this).attr('action'),
$(this).serialize(),
function(data, textStatus, jqXHR){
if(data=="200"){
console.log('post OK'); //this shows in console!
window.tmp_input_ok = true;
} else{
console.log('not OK');
}
}
).fail(function() {
console.log('POST fail)');
});
}
//however, window.tmp_input_ok is false here so the textbox does not empty:
if(window.tmp_input_ok == true) $textbox.val('');
else console.log('input not ok :('); //and this is outputted to console
});
的淵源,也只是一個var tmp_input_ok = false
初始化,然後工作與tmp_input_ok
變量,但它沒有工作,所以我嘗試了全球窗口...也不管用...我開始絕望。
'$ .post()'調用是異步的。該函數會立即返回,但是直到收到HTTP響應時纔會調用傳入的回調。 – Pointy