简体   繁体   English

jQuery在Do-While循环中无法按预期工作

[英]Jquery Not Working as Expected in a Do-While Loop

I have a webpage which lists hotel rooms and then optionally displays an availability calendar for each room. 我有一个列出酒店房间的网页,然后有选择地显示每个房间的可用性日历。 My problem is that the calendar doesn't always display. 我的问题是日历并不总是显示。

The list of rooms is created with a PHP do-while loop. 房间列表是通过PHP do-while循环创建的。 Each entry contains some information and a link to view the availability calendar. 每个条目都包含一些信息和一个查看可用性日历的链接。

<a href="javascript:;" data-roomid="<?php echo $room_id ?>" class="click">See Calendar</a>

Then the reveal of the calendar is triggered by this jquery to open up in a div of class hiddencal and with the same data-roomid as the link: 然后,这个jquery触发日历的显示,以在一个hiddencal类的div中打开,并且其data-roomid与链接相同:

$(document).ready(function() {
    $("a.click").on('click', function() {
        var s = $(this);
        var roomid = $(this).attr("data-roomid");
        $(".hiddencal[data-roomid='" + roomid + "']").load("availcalendar.php?hid=<?php echo $hid;?>&room_id=" + roomid + "&month=<?php echo $ArrMonth;?>&year=<?php echo $ArrYear;?>")
        .slideToggle(1000, function() {
            s.html(s.text() == 'See Calendar' ? 'Hide Calendar' : 'See Calendar');
        });
    });
});

Let us say there are four rooms on the list. 让我们说清单上有四个房间。 If I click the link on the first room, the calendar displays, but if I follow that by clicking the link on the second room the slideToggle works but there is no calendar. 如果我单击第一个房间上的链接,则显示日历,但是如果我单击第二个房间上的链接,则会显示slideToggle,但没有日历。

Conversely, if I start with the second room and then go to the first room I can get both calendars to display, but not the third or fourth. 相反,如果我从第二个房间开始,然后转到第一个房间,则可以显示两个日历,但不能显示第三个或第四个日历。 So in effect I can go through the calendars from bottom to top, but no top to bottom. 因此,实际上,我可以从下至上浏览日历,但无从上到下。

I have tried echoing the variables in the availcalendar.php file to see if some data is being dropped but I can't trace the problem. 我尝试回显availcalendar.php文件中的变量,以查看是否删除了某些数据,但我无法跟踪问题。 Is there something I'm missing that is obvious to a more skilled eye than mine? 我所缺少的东西比我的眼睛更明显吗?

EDIT2 GaetanoM was right, but that was only one of two errors, so I struggled to work out what was going on. EDIT2 GaetanoM是正确的,但这只是两个错误之一,因此我努力弄清发生了什么。 I have finished up with this code, very similar to GaetanoM's suggestion: 我已经完成了这段代码,非常类似于GaetanoM的建议:

$(document).ready(function() {
$("a.click").on('click', function() {
    var s = $(this);
    var roomid = $(this).attr("data-roomid");
$(".hiddencal[data-roomid='" + roomid + "']").load("availcalendar.php?hid=<?php echo $hid?>&room_id=" + roomid + "&month=06&year=2019").slideToggle(1200, function() {
        s.html(s.text() == 'See Calendar' ? 'Hide Calendar' : 'See Calendar');
    });
});
});

There was another problem in the availcalendar.php file, and once I corrected them both, all was well. availcalendar.php文件中还有另一个问题,一旦我将它们都纠正,一切都很好。 Thanks to everyone for the suggestions. 感谢大家的建议。

Your issue is in this line: 您的问题在以下行中:

$(".hiddencal[data-roomid='" + roomid + "']").load("availcalendar.php?hid=<?php echo $hid;?>&room_id=" + roomid + "&month=<?php echo $ArrMonth;?>&year=<?php echo $ArrYear;?>")

.load( url [, data ] [, complete ] ) : Load data from the server and place the returned HTML into the matched elements. .load(url [,data] [,complete]) :从服务器加载数据并将返回的HTML放入匹配的元素中。

complete : A callback function that is executed when the request completes. complete :在请求完成时执行的回调函数。

Hence, in order to run the slideToggle you need to use the complete callback: 因此,为了运行slideToggle,您需要使用完整的回调:

$(".hiddencal[data-roomid='" + roomid + "']").load("YOUR URL", function() {
    s.slideToggle(1000, function() {
        s.html(s.text() == 'See Calendar' ? 'Hide Calendar' : 'See Calendar');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM