Javascript $.post函数能否覆盖父函数中的变量
上周我开始玩Java脚本,所以我的知识是。。。非常有限。请对我耐心点:-) 我正在编写一个简单的预订系统,在MySql中存储日期和用户ID。它检查给定的日期是否已经预订(如果是,则isTicket.php返回UserId,如果仍然免费,则返回0)。 由于可以选择一个天数范围,并且我不想发送多个警告,因此,如果在所选的任何一天已有其他用户预订,我将变量otherEventFound设置为false 如下图所示,我尝试使用post函数的“out side”变量,有两种可能性:Javascript $.post函数能否覆盖父函数中的变量,javascript,jquery,post,Javascript,Jquery,Post,上周我开始玩Java脚本,所以我的知识是。。。非常有限。请对我耐心点:-) 我正在编写一个简单的预订系统,在MySql中存储日期和用户ID。它检查给定的日期是否已经预订(如果是,则isTicket.php返回UserId,如果仍然免费,则返回0)。 由于可以选择一个天数范围,并且我不想发送多个警告,因此,如果在所选的任何一天已有其他用户预订,我将变量otherEventFound设置为false 如下图所示,我尝试使用post函数的“out side”变量,有两种可能性: 1) 如果我的脚本包含
1) 如果我的脚本包含行>>警报(“otherEventFound…当您执行$.post时,将启动一个异步AJAX请求,稍后将调用您的内联函数。由于您不希望在返回固定数量的异步请求之前执行某段代码,因此您必须跟踪已完成的请求数 它与警报“起作用”的唯一原因是,警报会插入一个暂停,直到您应答为止,此时AJAX调用完成 并执行内联函数 您基本上希望修改代码,如下所示:
var otherEventFound = false;
var do_the_booking = false;
var completeRequests = 0;
for ( var i = myStart.getDfJ(); i <= myEnd.getDfJ(); i++)
{
// do something
// and ask server if there is other event on this day
$.post("isTicket.php", { theDay: strID },
function(answ){
completeRequests++;
if ( parseInt(answ) == 0 ){
do_the_booking = true;
}
else {
if ( !(parseInt(answ) == currentUserId) ){
otherEventFound = true;
}
}
if (completeRequests == myEnd.getDfJ()) {
postProcessing();
}
}
);
}
function postProcessing() {
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" ...
}
}
}
var otherEventFound=false;
var do_the_booking=假;
var completeRequests=0;
对于(var i=myStart.getDfJ();i,当您执行$.post时,将启动一个异步AJAX请求,稍后将调用您的内联函数。由于您不希望在返回固定数量的异步请求之前执行某段代码,因此您必须跟踪已完成的请求数量
它与警报“起作用”的唯一原因是,警报会插入一个暂停,直到您应答为止,此时AJAX调用完成
并执行内联函数
您基本上希望修改代码,如下所示:
var otherEventFound = false;
var do_the_booking = false;
var completeRequests = 0;
for ( var i = myStart.getDfJ(); i <= myEnd.getDfJ(); i++)
{
// do something
// and ask server if there is other event on this day
$.post("isTicket.php", { theDay: strID },
function(answ){
completeRequests++;
if ( parseInt(answ) == 0 ){
do_the_booking = true;
}
else {
if ( !(parseInt(answ) == currentUserId) ){
otherEventFound = true;
}
}
if (completeRequests == myEnd.getDfJ()) {
postProcessing();
}
}
);
}
function postProcessing() {
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" ...
}
}
}
var otherEventFound=false;
var do_the_booking=假;
var completeRequests=0;
对于(var i=myStart.getDfJ();i使用new Boolean()
是一个坏主意,它的行为方式出人意料,只需在var
声明中指定一个布尔文本true
或false
。在条件语句中使用like不能可靠地使用它,if(new Boolean(false)){
将始终计算为true,因为对象被强制为true。JavaScript不使用严格类型,您不必在存储变量值之前声明要存储的变量值类型。使用new Boolean()
是一个坏主意,它的行为方式出乎意料,只需在var
声明中指定一个布尔文本true
或false
。在条件语句中使用like不能可靠地使用它,if(new Boolean(false)){}
将始终计算为true,因为对象被强制为true。JavaScript不使用严格的类型,您不必在存储变量值之前声明要存储的变量值类型。感谢您的回答和解释。但是,此解决方案无法解决在for循环中生成多个警告的问题。如果omebody选择f.e.5天,其中3天已经预订,然后此代码将启动3次警报“没有…”和两次“您想…”。有没有办法克服这种行为?我将整个预订移动到了setTicket.php
。此脚本还可以处理多天以及其他用户的现有票据。非常感谢您的帮助。感谢您的回答和解释。但是此解决方案无法解决在中生成多个警告的问题-循环。如果有人选择f.e.5天,并且其中3天已经预订,则此代码将启动3次警报“没有…”和两次“您想…”。有没有办法克服这种行为?我将整个预订移动到了setTicket.php
。此脚本还可以处理多天以及其他用户的现有票据。非常感谢您的帮助。