javascript - 有没有办法从 PHP while 循环中获取所有获取的 id?

标签 javascript php jquery html ajax

我想知道是否可以从 PHP while 循环获取所有获取的 id,以便在 jQuery 中使用它们来实现翻转效果?

while ($fetch_locomotion = mysqli_fetch_assoc($result_locomotion))
{
    $id = $fetch_locomotion['id'];

    echo 
    '<a href="index.php?price='.$hash.'" style="text-decoration: none;" id="object_a_jquery">
        <div id="'.$id.'" style="perspective:1000px;height:100%;width:13%;display:inline-table;">
            <div class="front">
                <div id="object_list_price_bg" style="background-image:url(images/bg_price_f.png);"></div>
            </div>
            <div class="back">
                <div id="object_list_price_bg_b" style="background-image:url(images/bg_price.png);"></div>
            </div>
        </div>
    </a>';
}


<script>$(<?php echo $id;?>).flip();</script>

最佳答案

您可以将它们转储到数组中并在以后使用它们。

// do this before entering the loop so it isn't overwritten
$ids = [];
// Do this part inside of the while loop
$ids[] = $fetch_locomotion ['id'];

?>

/* do this part in the html section you wish to execute the javascript code */
<script>
jQuery(function ($) {
    <?php foreach ($ids as $id): ?>
        <?php echo "$('#". $id . "').flip();\n"; ?>
    <?php endforeach; ?>
});
</script>

编辑您的确切代码:

$ids = [];
while ($fetch_locomotion = mysqli_fetch_assoc($result_locomotion))
{

    $ids[] = $fetch_locomotion['id'];

    echo 
    '<a href="index.php?price='.$hash.'" style="text-decoration: none;" id="object_a_jquery">
        <div id="'. $fetch_locomotion ['id'] .'" style="perspective:1000px;height:100%;width:13%;display:inline-table;">
            <div class="front">
                <div id="object_list_price_bg" style="background-image:url(images/bg_price_f.png);"></div>
            </div>
            <div class="back">
                <div id="object_list_price_bg_b" style="background-image:url(images/bg_price.png);"></div>
            </div>
        </div>
    </a>';
}


<script>
jQuery(function ($) {
    <?php foreach ($ids as $id): ?>
        <?php echo "$('#". $id . "').flip();\n"; ?>
    <?php endforeach; ?>
});
</script>

关于javascript - 有没有办法从 PHP while 循环中获取所有获取的 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783840/

相关文章:

javascript - 嵌入数组的 MongoDB 平均值作为额外字段

php - MySQL datetime数据类型获取特定日期范围内的访问者总数

php - 使用 Laravel 和 AngularJs

php - 从各种表格的复选框中插入值?

jquery - 搜索框中的 Ajax/jQuery 自动完成 (Shopify)

jquery - Cycle2 显示前一个 div 的背景

javascript - `$().clone(true)` 破坏 Chrome 扩展中的事件处理程序(适用于 jsbin)

javascript - 从选择框中获取所有选项(选定和未选定)

javascript - 根据属性从数组中获取对象

Javascript - 连接字符串和 int 不起作用(Lightswitch HTML)