[英]Delete an image only when user clicks in my “yes” link of my own dialog
I have a gallery with some images, and I have a link in each image to delete it: 我有一个包含一些图像的画廊,并且每个图像都有一个链接将其删除:
<div class="galery">
echo '<ul>';
while ($result = $read->fetch(PDO::FETCH_ASSOC)){
echo '<li id="'.$result['id'].'">';
echo '<img src="'.$result['img'].'" />';
echo '<a href="#" id="'.$resul['id'].'" class="galerydel">Delete</a>';
echo '</li>';
}
echo '</ul>';
</div>
And then I have a jQuery script to delete my image using ajax: I get my id of each image an then I have my ajax $.post: 然后我有一个jQuery脚本使用ajax删除我的图像:得到每个图像的ID,然后得到ajax $ .post:
var url = 'deleteImages.php';
$('.galery ul').on('click','.galerydel',function(){
var delid = $(this).attr('id');
$.post(url,{action:'del_images',image:delid},function(){
});
return false;
});
And then I have my php.file, where I get my image id $imageId = $_POST['image'];
然后我有我的php.file,在这里我得到了图像ID $imageId = $_POST['image'];
and action $_POST['action'];
和动作$_POST['action'];
and then I do my delete in my gallery table where id is = $imageId
. 然后在ID为= $imageId
图库表中进行删除。
And this is working fine, but now I want to have a dialog message asking user if he have sure that wants to remove image, so I have a div for a dialog like this: 这项工作正常,但现在我想弹出对话框询问用户是否确定要删除图像,因此我为对话框设置了一个div,如下所示:
<div class="delete_dialog">
<div class="delete">
<p>Do you really want to delete this image?</p>
<a href="#">Yes</a>
<a href="#">No</a>
</div>
</div>
And then I want to show this div when I click in my link to delete each image: 然后,当我单击链接以删除每个图像时,我想显示此div:
var url = 'deleteImages.php';
$('.galery ul').on('click','.galerydel',function(){
var delid = $(this).attr('id');
$('.delete_dialog').fadeIn("slow",function(){
('.delete').fadeIn("slow");
});
$.post(url,{action:'del_images',image:delid},function(){
});
return false;
}); ´
And now, when I click in my delete link Im open my dialog, but Im also removing my news without answer to my dialog. 现在,当我单击“删除”链接时,我打开了对话框,但是我也删除了我的新闻,而没有回答对话框。
Do you know how can I use my dialog, if I click in yes I do my delete, but If I click in no I just close my dialog? 您知道如何使用对话框吗?如果单击“是”,则执行删除操作,但是如果单击“否”,则仅关闭对话框?
Im reading about this for hours, and I just saw examples using jQuery dialog, and its easy because we can use buttons (yes and no). 我花了好几个小时阅读这些内容,而且我刚刚看到了使用jQuery对话框的示例,它很简单,因为我们可以使用按钮(是和否)。
And I also saw examples with if(confirm(Do you really want to delete this image?){my ajax here}
and it is also easy to do. 我还看到了if(confirm(Do you really want to delete this image?){my ajax here}
,它也很容易实现。
But Im trying to do with my own dialog, and like this I really dont know how we can do this. 但是我试图处理我自己的对话框,并且像这样,我真的不知道我们该怎么做。
Can you please give me some help?? 你能给我些帮助吗?
What Im trying: 我正在尝试:
var url = 'deleteImages.php';
$('.galery ul').on('click','.galerydel',function(){
var delid = $(this).attr('id');
$('.delete_dialog').fadeIn("slow",function(){
('.delete').fadeIn("slow");
});
$("a#delete").click(function(){
$('.galerry ul li[id="'+ delid +'"]').css('background','red');
$.post(url,{action:'del_images',image:delid},function(){
window.setTimeout(function(){
$('.galery ul li[id="'+ delid +'"]').fadeOut("slow");
},500);
$('.delete').fadeOut("fast",function(){
$('.delete_dialog').fadeOut("fast");
});
});
$("a#toggleDialog").click(function(){
$('.delete').fadeOut("fast",function(){
$('.delete_dialog').fadeOut("fast");
});
});
});
Add some identificators to you "Yes/No" links (ids, attributes or something else): 向您添加一些标识符“是/否”链接(ID,属性或其他内容):
<div class="delete_dialog"> <div class="delete"> <p>Do you really want to delete this image?</p> <a href="#" id="delete">Yes</a> <a href="#" id="toggleDialog">No</a> </div> </div>
Add handler to links like: 将处理程序添加到链接,例如:
$("a#delete").click(function(){ //your post method }); $("a#toggleDialog").click(function(){ //hide dialog });
Change your open dialog handler to show dialog only: 将打开的对话框处理程序更改为仅显示对话框:
$('.galery ul').on('click','.galerydel',function(){ var delid = $(this).attr('id'); $('.delete_dialog').fadeIn("slow",function(){ ('.delete').fadeIn("slow"); }); });
PS also you should pass the delid
variable to "Yes" handler. PS另外,您还应该将delid
变量传递给“是”处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.