对于某些情况:我正在建立一个博客系统,用户可以看到他们添加的所有文章。为此,我恢复了username = $ thatUser的文章的ID,并使用foreach循环来显示。作品查找除了我希望用户能够删除这些文章。一点点“你确定吗?”消息显示在灯箱之前,这就是问题所在。单击按钮以显示灯箱时,它仅显示最后一个ID。请参阅以下代码:
首先是灯箱代码(jQuery):
$(document).ready(function()
{
$('a#deleteArticleFormJS').click(function()
{
$('#background,#deleteArticle').fadeIn('fast');
});
$('a#hideDeleteArticle, a#cancelDeleteArticle').click(function()
{
$('#background,#deleteArticle').fadeOut('fast');
});
});
然后PHP / html代码:
<?php
//articles(); is a basic SQL query to recover the ID
$Articles = articles();
foreach($Articles as $Article)
{
?>
<!-- Recover the thumnail of the articles, the ID is correct. -->
<img src="avatar/<?php echo $Article['ID']?> - Avatar.jpg">
<!-- Link that shows the lightbox with the "Are you sure" message, the ID is correct. -->
<a href="#<?php echo $Article['ID'];?>" id="deleteArticleFormJS">Delete</a>
<!-- The "lightbox". -->
<div id="deleteArticle">
//*** PROBLEM: only show the LAST ID?! ***//
Delete <?php echo $Article['ID'];?>
</div>
<!-- End of the foreach loop -->
<?php
}
?>
我的问题是:
1 - 如何在灯箱中获取正确的ID?
如果需要更多详细信息,代码或任何内容,请询问。
P.S:我不确定这是否是正确的“StackExchange”网站。如果没有,我道歉。
答案 0 :(得分:0)
试试这个:
$(document).ready(function()
{
$('a.deleteArticleFormJS').click(function()
{
$('#background,#deleteArticle'+$(this).data('article-id')).fadeIn('fast');
});
$('a.hideDeleteArticle, a.cancelDeleteArticle').click(function()
{
$('#background,#deleteArticle'+$(this).data('article-id')).fadeOut('fast');
});
});
<?php
//articles(); is a basic SQL query to recover the ID
$Articles = articles();
foreach($Articles as $Article)
{
?>
<!-- Recover the thumnail of the articles, the ID is correct. -->
<img src="avatar/<?php echo $Article['ID']?> - Avatar.jpg">
<!-- Link that shows the lightbox with the "Are you sure" message, the ID is correct. -->
<a href="#<?php echo $Article['ID'];?>" class="deleteArticleFormJS" data-article-id="<?php echo $Article['ID']; ?>" >Delete</a>
<!-- The "lightbox". -->
<div class="deleteArticle" id="deleteArticle<?php echo $Article['ID']; ?>" >
//*** PROBLEM: only show the LAST ID?! ***//
Delete <?php echo $Article['ID'];?>
</div>
<!-- End of the foreach loop -->
<?php
}
?>