简体   繁体   English

在循环内切换脚本仅适用于第一次迭代

[英]Toggle script inside a loop only works for first iteration

Here is my php/javascript code. 这是我的php / javascript代码。 I have tried to write the script inside the loop but it doesn't work. 我试图在循环中编写脚本,但它不起作用。 This generates two buttons, one for local team, while processing local team, and the second for road team, while processing road team. 这会生成两个按钮,一个用于本地团队,一个用于处理本地团队,另一个用于道路团队,同时处理道路团队。 These buttons show information contained in a div of a different class. 这些按钮显示包含在不同类的div中的信息。 For some reason, it works only for the local team, which its the first iteration of the loop. 出于某种原因,它仅适用于本地团队,它是循环的第一次迭代。

I have read a lot about people using id's instead of classes, but this is not my case. 我已经阅读了很多关于使用id而不是类的人,但这不是我的情况。 I am quite lost. 我很失落。

Thank you very much for your help. 非常感谢您的帮助。

<script>
    $(".showavgloc").click(function(){
        event.preventDefault();
        $(".averageloc").slideToggle("slow");
    });
    $(".showavgvis").click(function(){
        event.preventDefault();
        $(".averagevis").slideToggle("slow");
    });
</script>
<?php
foreach ($teams as $team) { 
    if ($page=="game") { 
        if ($team==$locteam) { 
            echo "<button class='showavgloc'>Show average player</button>";
        }
        if ($team==$visteam) {
            echo "<button class='showavgvis'>Show average player</button>";
        } 
    }   
    if ($team==$locteam) {
        echo "<div class='averageloc' style='display:none'>This div has to be shown/hidden for local team</div>";
    }   
    if ($team==$visteam) {
        echo "<div class='averagevis' style='display:none'>This div has to be shown/hidden for road team</div>";
    }   
}
?>

Try to do this: 尝试这样做:

after that php code, and before close it ( before this "?>"), do an "echo" that contains: 在那个PHP代码之后,在关闭它之前(在这个“?>”之前),做一个包含以下内容的“echo”:

<script>
    $(".showavgloc").click(function(){
        event.preventDefault();
        $(".averageloc").slideToggle("slow");
    });
    $(".showavgvis").click(function(){
        event.preventDefault();
        $(".averagevis").slideToggle("slow");
    });
</script>

and try to use it as you want. 并尝试使用它你想要的。 This might help in some cases, because when you use the jQuery library and then you generate html throw code, the DOM keeps with the "initial" version an jQuery can't work properly because never find the controls. 在某些情况下这可能会有所帮助,因为当您使用jQuery库然后生成html throw代码时,DOM会保留“初始”版本,jQuery无法正常工作,因为从未找到控件。 The right order is : Print controls and then bind jQuery events. 正确的顺序是:打印控件然后绑定jQuery事件。

Good luck 祝好运

Your code seems to work fine when everything is included: 当包含所有内容时,您的代码似乎工作正常:

 $(".showavgloc").click(function(){ event.preventDefault(); $(".averageloc").slideToggle("slow"); }); $(".showavgvis").click(function(){ event.preventDefault(); $(".averagevis").slideToggle("slow"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button class='showavgloc'>Show average player</button> <button class='showavgvis'>Show average player</button> <div class='averageloc' style='display:none'>This div has to be shown/hidden for local team</div> <div class='averagevis' style='display:none'>This div has to be shown/hidden for road team</div> 

My guess is that something else is amiss. 我的猜测是其他东西不对劲。 Inspect your page to ensure all elements are printed properly by your PHP. 检查您的页面以确保PHP正确打印所有元素。 If everything seems fine, you could try using the jQuery "on" function, which should watch for new elements that match the selector. 如果一切看起来都很好,你可以尝试使用jQuery“on”函数,该函数应该监视与选择器匹配的新元素。 https://api.jquery.com/on/ https://api.jquery.com/on/

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

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