php - jQuery UI 多对话框和 PHP

标签 php jquery ajax jquery-ui jquery-ui-dialog

我在一个页面上有多个对话框并将它们链接到 PHP 生成的 ID 时遇到一些问题。我希望将多个图像链接到包含内容的对话框...这是我的代码:

PHP:

<div id="children">
<?php 


$children = array_values(get_pages(array('child_of' => $post->ID)));

foreach( $children as $ch => $child ){
    echo '<div id="dialog-'.$child->ID.'" title="Basic dialog">';
    echo $child->post_content;
    echo '</div>';
}

foreach( $children as $ch => $child ){
    $attachments = get_posts(array('numberposts'=> 1, 'post_parent' => $child->ID, 'post_type' => 'attachment', 'order' => 'DESC', 'orderby' => 'post_date'));
    //print_r($attachments);
    foreach( $attachments as $a => $attachment ){
        echo '<a href="#opener-'.$child->ID.'" id="opener-'.$child->ID.'" >';
        echo '<img class="attachment-thumb" src="'.$attachment->guid.'" width="150" height="150" />';
        echo '</a> ';
    }
}
    //print_r($children)    
?>

现在我意识到我的 jQuery 正在生成每个 id 1,2,3 而不是实际的 PHP ID,但我不知道如何设置它以便 jQuery 将链接到正确的对话框和打开器。我需要使用 Ajax 吗?

jQuery:

<script>
$(function() {
var options = {
        autoOpen: false,
        width: 'auto',
        modal: true
};
var num = 1;
$("#children").each(function() {
        var dlg = $('#dialog-' + num).dialog(options);
        $('#opener-' + num).click(function() {
                dlg.dialog("open");
                return false;
        });
        num = num + 1;
});
});
</script>
</div>

最佳答案

$("#children").each 仅针对一个“children”div 运行一次。因此,在此函数中 num 永远不会大于 1。您可能需要 $("#children div").each,它将针对 'children' 的所有 div 子级执行。

(从技术上讲,num 变成 2(1 执行后)...但在 2 时永远不会执行。)

关于php - jQuery UI 多对话框和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7500694/

相关文章:

php - mysql - 如何选择类似的记录?

php - 检查用户是否在另一个系统中登录问题

javascript - 在html中找到指定字符串后动态插入新行

javascript - 验证单选按钮 : javascript

PHPdoc:记录可链接的方法?

PHP 替代 session_is_registered

jquery - 从 $(document).ready(function()) 调用函数

Javascript 'Element must be a table' <表 ID ="mytable">

javascript - AJAX 请求保存到数据库但警报失败

javascript - 页面切换时更新事件监听器