上週我已經開始使用javascripts了,所以我的知識非常有限。請耐心等待我:-)
我正在寫一個簡單的預訂系統,在MySql中存儲日期和用戶ID。它檢查給定日是否已經預訂(如果是,則比isTicket.php返回UserId,如果它仍然是空閒的而不是返回0)。 由於可以選擇一個天數範圍,而且我不想發送多個警告,所以如果在任何選定日期其他用戶已被預訂,我將變量otherEventFound設置爲false。
如下圖所示,我試圖用這個變量「出邊」功能後,有兩種可能性:
1)如果我的腳本包含線>>警報(「otherEventFound .. 。< <它的工作原理。
2)如果我刪除此行,事實並非如此。
我有點迷失方向。有人可以解釋爲什麼這額外的警告線是非常重要的,一般多是否有可能從post.success函數覆蓋父函數中的變量?
如果純粹的運氣比警告線更有效,那麼在JavaScript中使用它的正確方法是什麼?
parent function
...
var otherEventFound = new Boolean();
var do_the_booking = new Boolean();
otherEventFound = false;
do_the_booking = false;
for (var i = myStart.getDfJ(); i <= myEnd.getDfJ(); i++)
{
// conver i to MySQL format yyyy-mm-dd
var milliseconds = 1000 * 60 * 60 * 24 *i;
var j = new Date(milliseconds);
var strID = j.toYMD();
// and ask server if there is other event on this day
$.post("isTicket.php", { theDay: strID },
function(answ){
if (parseInt(answ) == 0){
do_the_booking = true;
}
else {
if (!(parseInt(answ) == currentUserId)){
otherEventFound = true;
}
}
}
);
}
alert ("otherEventFound " + otherEventFound + " do_the_booking " + do_the_booking);
if (otherEventFound==true) {
alert ("There is not yours event booked on this day.");
do_the_booking=false;
};
if (do_the_booking==true){
var x=window.confirm("Do you want to book on this/these day/s?")
if (x) {
// ... do something like $.post("setTicket.php" ...
}
}
使用'新的布爾()'是一個壞主意,它的行爲以意想不到的方式,只是分配一個布爾值TRUE;或'假在'var'聲明中。使用['Boolean'對象](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean)就像你不能在條件語句中可靠地使用它,'if(new Boolean(false )){}'將總是計算爲真,因爲對象被強制爲真。 JavaScript不使用嚴格的輸入,在存儲之前,您不必聲明要存儲哪種類型的變量值。 –