我在一个页面上有多个对话框并将它们链接到 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/