简体   繁体   English

仅当图像列表中有多个图像时,才显示“删除”链接

[英]Show my link “Delete” only if I have more than one image in my image list

Im deleting banner images using jQuery and Ajax. 我使用jQuery和Ajax删除横幅图像。

But if I have just one image I dont want to delete it. 但是,如果我只有一张图像,我不想删除它。

I have a list of images, and for each image I have this link: 我有一个图像列表,对于每个图像,我都有此链接:

echo '<a class="delete j_bannerdelete" id="'.$rslt_banner['id'].'" href="#">Delete</a> ';

And I was thinking something about tihs, just show "Delete" link if my row count was more than 1. 我在想些什么,如果我的行数超过1,请显示“删除”链接。

if($read_ban->rowCOunt() >1{
     echo '<a class="delete j_bannerdelete" id="'.$rslt_banner['id'].'" href="#">Delete</a> ';
}

But like this, for example If I have 2 images, I delete one, and as Im deleting with ajax, my page dont refresh, so I have my last image with my "Delete" link and so I can delete it. 但是像这样,例如,如果我有2张图像,我将删除一张,而由于我用ajax删除,我的页面不会刷新,因此我的最后一张图像带有“删除”链接,因此可以删除它。

Do you know aa simple way to solve this? 您知道解决这个问题的简单方法吗?

My delete code: 我的删除代码:

$(function(){
    var banner_id = null;
    $("a#no").click(function(event){
        event.preventDefault();
        $('.confirm').fadeOut("slow",function(){
            $('.delete_dialog').fadeOut("slow"); 
        });
        $('.bannerli li[id="'+ banner_id +'"]').css('background','#f5f5f5');
        banner_id = null;
    });
    $("a#delete").click(function(event){
        event.preventDefault();
        if (!banner_id) return;
        $.post(url,{action:'ban',id: banner_id},function(){
            window.setTimeout(function(){
                $('.bannerli li[id="'+ banner_id +'"]').fadeOut("slow");
            },500);
            $('.confirm').fadeOut("fast",function(){
                $('.delete_dialog').fadeOut("fast"); 
            });
        });
    });
    $('.bannerli').on('click','.j_bannerdelete',function(){
        banner_id = $(this).attr('id');
        $('.bannerli li[id="'+ banner_id +'"]').css('background','red');
        $('.delete_dialog p').text('Are you sure you want to remove this banner?');
        $('.delete_dialog').fadeIn("slow",function(){
            $('.confirm').fadeIn("slow");
        });
        return false;
    })
});

I created a simplified solution here: http://jsfiddle.net/wgjrnw7m/1/ . 我在这里创建了一个简化的解决方案: http : //jsfiddle.net/wgjrnw7m/1/ It should give you an idea of how to do it. 它应该使您知道如何执行此操作。

Basically you want to count how many items you have: 基本上,您想统计一下您拥有多少物品:

if($('.bannerli').length <= 1) { /* Hide delete */ }

And you want to do this check everytime an item is deleted. 并且您想在每次删除项目时进行此检查。 If items can also be added, you also need to check if you have more than one item and in that case show all the delete-buttons again. 如果还可以添加项目,则还需要检查是否有多个项目,在这种情况下,请再次显示所有删除按钮。

count how many banners are still on the page with a jQuery selector. 用jQuery选择器计算页面上还有多少横幅。 than only show the delete functionality if 比仅在以下情况下显示删除功能

if ( $('.j_bannerdelete').length > 1 ) {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM