简体   繁体   English

如何从php传递数据以在javascript中对其进行初始化?

[英]How to pass data from php to initialize it in javascript?

I am trying to display some football fixtures but I am struggling with opening Modals. 我正在尝试展示一些足球器材,但是我在尝试打开Modals时很挣扎。 In each game from each week I want to open one modal where I should display more informations about the respective game. 在每周的每场比赛中,我都想打开一个模式,在该模式下应显示有关相应游戏的更多信息。 I need to initialize every modal by the time it is created. 我需要在每个模态创建时对其进行初始化。 I don't know how to pass the modal data from the PHP to javascript. 我不知道如何将模式数据从PHP传递到javascript。 With the code below only Modals from 1-5 will work, the other will create an error when clicked. 在下面的代码中,只有1-5的模态有效,其他的单击时会产生错误。

PHP modal create PHP模式创建

$i = 1;
$j = 1;
foreach ($games as $rounds) {
echo "<div id='et$i' class='tabcontent'>";
echo "<ul class='collection'>";

foreach ($rounds as $match) {
    echo "<li class='collection-item'>";
        echo "<div class='row'>";
        echo "<div class='center col s4'>{$match[1]}</div>";
        echo "<div class='center col s1'>-</div>";
        echo "<div class='center col s4'>{$match[0]}</div>";
        echo "<a href='#meci$j' class='secondary-content' onclick=\"$('#modal$j').modal('open');\"> <i class='material-icons'>assessment</i> </a>";
        echo "</div>";

        echo "<div id='modal$j' class='modal'>";
        echo "<div class='modal-content'>";
        echo "<div class='row'>";
        echo "<div class='center col s4'>$match[1]</div>";
        echo "<div class='center col s1'>-</div>";
        echo "<div class='center col s4'>$match[0]</div>";
        echo "</div>";

        echo "</div>";
        echo "</div>";
    echo "</li>";
    $j++;
}
echo "</ul>";
echo "</div>";
$i++;
}

Javascript initialization JavaScript初始化

$(document).ready(function() {
    $('#modal1').modal();
    $('#modal2').modal();
    $('#modal3').modal();
    $('#modal4').modal();
    $('#modal5').modal();
});

Since you've given all the modals the same class, you can use that to initialize them. 由于您为所有模态赋予了相同的类,因此可以使用它来初始化它们。

$(document).ready(function() {
    $(".modal").modal();
})

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

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