gpt4 book ai didi

javascript - JS : Changing variable wont apply on next

转载 作者:行者123 更新时间:2023-12-02 20:26:47 26 4
gpt4 key购买 nike

我有这个:

$('#albumPhotoNext').live('click', function () {
var currentDate = '<?php echo $grab["date"]; ?>';
$.ajax({
url: "photo.php",
type: "post",
data: { currentDate : currentDate },
success: function(r){
currentDate = r.date;
}
});
});

我想将 currentDate 设置为 r.date,如果成功,那么下次您单击 #albumPhotoNext 时,currentDate 就是 r.date。

现在它给了我 $grab["date"];当我点击时,无论我是否将 currentDate 设置为 r.date 。

我只想$grab["date"];如果尚未设置为 r.date,则为“默认”

我该怎么做?

最佳答案

currentDate 是一个本地变量,因此它的值在每次调用时都会重置。

您需要使变量存在于外部作用域中,以便您的更改得以保留。

执行此操作的简单方法:

$(function() {
var currentDate = '<?php echo $grab["date"]; ?>';
$('#albumPhotoNext').live('click', function () {
$.ajax({
url: "photo.php",
type: "post",
data: { 'currentDate' : currentDate },
success: function(r){
currentDate = r.date;
}
});
});
});

关于javascript - JS : Changing variable wont apply on next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4717875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com