2011-09-21 84 views
2

我有一个问题,有一个页面上有多个对话框,并将它们链接到由PHP生成的ID。我想多图像是指向包含内容的对话......这里是我的代码:jQuery UI多对话框和PHP

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与而不是实际的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> 

回答

0

$( “#儿”)各只运行一次,对于一个 “孩子” 的div。因此在这个函数中num不会大于1。你可能想要$("#children div").each,这将执行'孩子'的所有div孩子。

(好吧技术上NUM成为执行2(后1)......但永远不会被同时执行2)