简体   繁体   English

仅当用户单击我自己对话框的“是”链接时才删除图像

[英]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"); 
    });
});
});
  1. 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>

  2. Add handler to links like: 将处理程序添加到链接,例如:

    $("a#delete").click(function(){ //your post method }); $("a#toggleDialog").click(function(){ //hide dialog });

  3. 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.

相关问题 用户单击链接时加载新图像 - Load new image when a user clicks on a link 在用户单击链接之前,如何防止加载图像滑块? - How can I prevent loading my image slider until a user clicks a link? 仅当图像列表中有多个图像时,才显示“删除”链接 - Show my link “Delete” only if I have more than one image in my image list 仅当用户单击相应的链接时,才如何呈现部分内容? - How to render a partial only when a user clicks the corresponding link? 用户单击链接问题时显示css背景图像 - css background image displays when user clicks link problem 是什么原因导致应该只有lastChild节点的图像节点对点击做出响应? - What is causing my image nodes to be responsive to clicks when only the lastChild node is supposed to? 使用原型js,如何在用户单击链接时打开对话框窗口并显示动态内容 - Using prototype js, how to open dialog window and display dynamic content when a user clicks link 当用户点击我的 iFrame 时如何调用这个函数? - How to call this function when the user clicks on my iFrame? 用户单击扩展程序上的按钮时如何插入文本? - How to insert text when the user clicks a button on my extension? 当用户单击按钮时,我希望我的ID晃动 - I would like my id to shake when the user clicks a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM