javascript - 如何删除上传的图片?

标签 javascript php jquery html css

我的 html 代码是这样的:

<input type='file' multiple/>
<?php
    for($i=0;$i<5; $i++) {
?>
    <div class="img-container" id="box<?php echo $i ?>">
        <button  style="display: none;" type="submit" class="btn btn-danger show-button">
            <i class="glyphicon glyphicon-trash"></i>
        </button>
    </div>
<?php
    }
?>

我的 javascript 代码是这样的:

    $(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 = '<img height="142" width="162" src='+e.target.result+'>';
        var IsImgAdded=false;
        $('.img-container').each(function(){
            if($(this).find('img').length==0 && IsImgAdded==false){
                $(this).append(imgTmpl);
                IsImgAdded=true;
                $(this).find('.show-button').show();
            }
        });     
    };

    $(".show-button").click(function(){
        $(this).find('img').hide()
    });

演示和完整代码如下:http://phpfiddle.org/main/code/uu9x-w50q

我尝试使用隐藏图像。但是没用

我该如何解决这个问题?

最佳答案

您应该使用 parent 方法来实现这一点,因为 image DOM 元素属于 .show- 的 parent按钮

$(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);
});

这里是 solution.

关于javascript - 如何删除上传的图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44089516/

相关文章:

php - MySQL 获取同一个表中存在对应行的行

javascript - 监听属性变化的事件

javascript - 如何将函数应用于具有相同类的所有新元素,但仅限于新创建的元素? (JS函数)

javascript - meteor :未捕获的 ReferenceError:未定义帐户

php - Imagecreatefromwebp() : WebP decode: fail to decode input data

PHP fatal error : Function name must be a string in

javascript - DRY - 如何组合这些 jQuery 函数?

javascript - 在 typeahead.js 中使用预取过滤器(使用基本选项)

javascript - 如何让三个JS中的TextGeometry跟随鼠标?

javascript - 在 JavaScript OnClick 中模拟单击事件会导致错误