在.done()中$.post之后,我如何使用$(this)



我希望这个函数在完成ajax$.post调用时更新div中的文本。一切都很好,除了.done调用$(this)中不再查看它列出的元素。

如有任何帮助,我们将不胜感激。

//Add or Remove time from Time Off
        $(".timeChange").on("click", function(){
            var id              =   $(this).val();
            var timeCommand     =   $(this).attr("data-command"); 
            var currentTime     =   $(this).siblings('.timeHold').text();       
            $.post("humanResourceFunctions.php/",
                {
                    command         :   "modifyLeaveTime",
                    employee        :   id,
                    addSubtract     :   timeCommand,
                    time            :   currentTime
                }).done(function(data) {
                    $(this).siblings('.timeHold').text(data); 
                });
        });

问题出现在上一个操作中,$(this).兄弟姐妹('.timeHold').text(data);

您只需要缓存对它的引用。

$(".timeChange").on("click", function(){
    var $this = $(this); //cache it

稍后…

.done(function(data) {
    $this.siblings('.timeHold').text(data); //note $this not $(this);
});

最新更新