简体   繁体   English

删除图像时如何显示加号按钮?

[英]How do I show back the plus button when delete image?

My html code is like this : 我的html代码是这样的:

<input type='file' multiple style="display: none;" id="upload-file" />
<?php   
    for($i=0;$i<5; $i++) { ?>

    <div class="img-container" id="box<?php echo $i ?>" data-status="0">
        <button type="submit" class="btn btn-primary upload-add-product"<?php 
            if($i!=0) echo' style="display:none;"'; ?> >
            <i class="glyphicon glyphicon-plus"></i>
        </button>

        <button  style="display: none;" class="btn btn-danger show-button">
            <i class="glyphicon glyphicon-trash"></i>
        </button>
    </div>

<?php } ?>

My javascript code is like this : 我的JavaScript代码是这样的:

...
$(document).on('click',".show-button",function(){
    var imgTmpl = '<div class="img-container">'+
               '<button  style="display: none;" type="submit" class="btn btn-danger show-button">'+
               '<i class="glyphicon glyphicon-trash"></i>'+
                '</button></div>';
    $(this).parent().remove();
    $('body').append(imgTmpl);
});  

Demo and full code is like this : http://phpfiddle.org/main/code/9kb1-r47h 演示和完整代码如下: http : //phpfiddle.org/main/code/9kb1-r47h

My problem is : when I uploaded 5 images. 我的问题是:当我上传5张图片时。 Then I delete 1 image. 然后删除1张图片。 Plus icon does not appear again. 加号图标不再出现。

How can I solve this problem? 我怎么解决这个问题?

Reason 原因

You didn't even add the + button back in. I'd also suggest you look at template literals to help with formatting and multi-lined code. 您甚至都没有添加+按钮。我还建议您查看模板文字,以帮助设置格式和多行代码。

You need to make sure that when you reach the end, that instead of having it set as none and waiting for something else to prompt it back to block or w/e, show it as block or w/e, intially 您需要确保到达终点时,不要将其设置为none并等待其他提示将其返回到block或w / e,而是将其显示为block或w / e

Updated parts: 更新的部分:

$(document).on('click',".show-button",function(){
    let pos;
    let i = 0;
    let parent = $(this).parent()[0];

    $(".img-container").each( function() {
        if (this == parent)
        {
            pos = i;
        }
        i++;
    });

    let show = (pos === 4) ? "block" : "none";

    var imgTmpl = '<div class="img-container" data-status="0">'+
                    '<button style="display: ' + show +'" type="submit" class="btn btn-primary upload-add-product" onclick="upload_click();">' +
                '<i class="glyphicon glyphicon-plus"></i>' +
                '</button>' +
               '<button  style="display: none;" type="submit" class="btn btn-danger show-button">'+
               '<i class="glyphicon glyphicon-trash"></i>'+
                '</button></div>';
    //console.log($(this).parent());
    $(this).parent().remove();

    $('body').append(imgTmpl);
});     

function upload_click()
{
    $("#upload-file").click();
}

http://phpfiddle.org/main/code/xzq8-h3ub http://phpfiddle.org/main/code/xzq8-h3ub

change your code like this , it is work for me, 像这样更改您的代码,对我来说是可行的,

    <style type="text/css">
    .img-container {
        width: 162px;
        height: 142px;
        border: 2px solid black;
        float: left;
        margin-right: 5px;
        position: relative;
    }
    .delete-button {
        position: absolute;
        left: 0;
    }

    .upload-add-product {
        margin-top: 55px;
        margin-left: 55px;
    }

    .img-container .show-button { position: absolute; top: 0; left: 0; }
</style>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<input type='file' multiple style="display: none;" id="upload-file" />
<div class="images-area">
<?php
    for($i=0;$i<5; $i++) { ?>

    <div class="img-container" id="box<?php echo $i ?>" data-status="0" data-index="<?=$i?>">
        <?php if ($i == 0): ?>
            <button type="submit" class="btn btn-primary upload-add-product">
                <i class="glyphicon glyphicon-plus"></i>
            </button>
        <?php endif; ?>
    </div>

<?php } ?>
</div>

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">

    $(document).on('click','.upload-add-product',function(){
        $("#upload-file").click();
    });

    $(function () {
        $(":file").change(function () {
            var noOfFiles = this.files.length;
            for(var i=0; i < noOfFiles; i++) {
                var reader = new FileReader();
                reader.onload = imageIsLoaded;
                reader.readAsDataURL(this.files[i]);
            }
        });
    });

    function imageIsLoaded(e) {
        var imgTmpl = '';
        var IsImgAdded=false;
        $('.img-container').each(function(){
            if($(this).find('img').length==0 && IsImgAdded==false){
                $(this).append(imgTmpl);
                IsImgAdded=true;

                $(this).attr('data-status',1);
                //$(this).find('.upload-add-product').hide();
                //$(this).find('.show-button').show();
                var i = $(this).closest('.img-container').data('index');
                var imgTmpl ='<img height="142" width="162" src='+e.target.result+'>'+
                           '<button type="button" class="btn btn-danger delete-button">'+
                           '<i class="glyphicon glyphicon-trash"></i>'+
                            '</button>';
                $('#box'+i).html('');
                $('#box'+i).append(imgTmpl);
                if(i<5) {
                    $('#box'+(i+1)).html('<button type="button" class="btn btn-primary upload-add-product">'+
                               '<i class="glyphicon glyphicon-plus"></i>'+
                                '</button>');
                }
                $('.img-container').each(function(){
                    if( $(this).attr('data-status') != 1){
                        $(this).find('.upload-add-product').show();
                        return false;
                    }
                })
            }
        });
    };


    $(document).on('click','.delete-button',function(){
        var i = $(this).closest('.img-container').attr('data-index');
        $('#box'+i).remove();
        $('.images-area').append('<div class="img-container" data-status="0"></div>');
        var blank = 0;
        $('.img-container').each(function(i){
            $(this).attr({'id':'box'+i,'data-index':i});
            if(($(this).attr('data-status') == 0) && (blank == 0)) {
                console.log("k");
                blank = i;
            }
        });
        if($('.img-container').find('.upload-add-product').length == 0) {
            $('#box'+blank).append('<button type="button" class="btn btn-primary upload-add-product">'+
                       '<i class="glyphicon glyphicon-plus"></i>'+
                        '</button>');
        }
    });


</script>

Demo is here 演示在这里

May be it helps you... 可能对您有帮助...

Prerequisite: Assign id to your delete button 先决条件:为您的删除按钮分配ID

Then On click of delete button, check if id =0 and accordingly apply style to your plus button as below. 然后在单击删除按钮时,检查id = 0并相应地将样式应用于加号按钮,如下所示。 Last step is to append newly created plus button to current div. 最后一步是将新创建的加号按钮添加到当前div。

$(".btn-danger").click(function(){

$id=this.id;
if($id!=0){
var r= $('<button type="submit" class="btn btn-primary upload-add-product" style="display:none;">
            <i class="glyphicon glyphicon-plus"></i>
        </button>');
}
else{
var r= $('<button type="submit" class="btn btn-primary upload-add-product">
            <i class="glyphicon glyphicon-plus"></i>
        </button>');
}


 $(this).parent().append($r);

});

Let me know if this doesn't work. 让我知道这是否无效。

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

相关问题 当我按下指定图像的按钮时,如何使图像不可见但显示? - How do I make an image be invisible but show when I press the button specifying the image? 如何在图像的右上角显示删除按钮 - how to show delete button in right corner of Image 单击按钮时如何删除2个文本框 - How do i delete 2 textboxes when i click a button 单击“后退”按钮时如何保留表单数据 - How do I keep form data when Back button is clicked 我在按钮上添加了一个“加号”。 当用户点击按钮时,“加号”被替换为“减号”并显示 div 内容 - I have added a “plus” sign to button. When the user clicks on the button, the “plus” sign is replaced with a “minus” sign and show the div content 我将如何 go 关于在 html 网站上创建可编辑的“块”,当按下加号按钮时显示? - How would I go about creating editable “blocks” on an html website that show up when a plus button gets pressed? 如何获得Google + +1按钮的计数器? - How do I get the counter of a google plus +1 button? 如何修复单击删除时不显示的删除模式 - How do i fix my delete modal that doesnt show up when i click delete 单击复选框或按钮时,我需要显示删除按钮 - I need to show a Delete Button when a check box or a button is clicked 单击按钮时如何显示div? - How do I show div when button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM