jQuery文件上传预览/删除图像

用户名

您可以在jsfiddle中找到工作文件

图像预览和删除功能很早就起作用了,但是由于某些库,我在html代码中做了一些更改。因此,由于Jquery中的父类调用问题,图像预览现在无法正常工作。请检查工作代码并帮助我解决。我使用具有相同名称数组的多个输入。所以我想在html代码中提到的下一个div中显示图像。我知道问题出在jquery。但是我无法解决。请帮忙。

function readURL() {
     var $input = $(this);
     if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
           reset($input.next('.delbtn'), true);
           $input.next('.portimg').attr('src', e.target.result).show();
           $input.after('<input type="button" class="delbtn removebtn" value="remove">');
        }
           reader.readAsDataURL(this.files[0]);
      }
    }
    $(".fileUpload").change(readURL);
    $("form").on('click', '.delbtn', function (e) {
        reset($(this));
    });

function reset(elm, prserveFileName) {
    if (elm && elm.length > 0) {
       var $input = elm;
       $input.next('.portimg').attr('src', '').hide();
       if (!prserveFileName) {
         $input.prev('.fileUpload').val("");
       }
         elm.remove();
       }
}

HTML代码

<form> <!--working code but it not needed now.-->
  <div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic type=file value="Choose a file"> 
        <img alt="your image" class=portimg src=#>
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div> 
<!--below code is not working. I need this to be work-->
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>

亚历克斯

请尝试以下版本:

HTML:

<form>
<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

</form>

JS:

function readURL() {
    var $input = $(this);
    var $newinput =  $(this).parent().parent().parent().find('.portimg ');
    if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            reset($newinput.next('.delbtn'), true);
            $newinput.attr('src', e.target.result).show();
            $newinput.after('<input type="button" class="delbtn removebtn" value="remove">');
        }
        reader.readAsDataURL(this.files[0]);
    }
}
$(".fileUpload").change(readURL);
$("form").on('click', '.delbtn', function (e) {
    reset($(this));
});

function reset(elm, prserveFileName) {
    if (elm && elm.length > 0) {
        var $input = elm;
        $input.prev('.portimg').attr('src', '').hide();
        if (!prserveFileName) {
            $($input).parent().parent().parent().find('input.fileUpload ').val("");
            //input.fileUpload and input#uploadre both need to empty values for particular div
        }
        elm.remove();
    }
}

JSFiddle:https ://jsfiddle.net/hxwmL1px/4/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

上传前删除图像预览

来自分类Dev

带有预览和删除选项的图像上传-Javascript / Jquery

来自分类Dev

Blueimp jQuery文件上传轨道:BMP图像预览问题

来自分类Dev

验证jQuery文件上传预览

来自分类Dev

带有回形针的jQuery文件上传,在上传之前显示预览图像

来自分类Dev

上传后预览图像

来自分类Dev

Vuetify-取消上传后删除预览图像

来自分类Dev

Vuetify-取消上传后删除预览图像

来自分类Dev

使用node.js预览上传的图像文件

来自分类Dev

jQuery功能可在上传之前对多个图像进行预览

来自分类Dev

Dropzone.js - 如果上传失败删除预览文件

来自分类Dev

多个图像上传和预览

来自分类Dev

多个图像上传和预览

来自分类Dev

Symfony删除使用文件系统上传的图像

来自分类Dev

在Dropper插件JQuery中删除了图像预览

来自分类Dev

jQuery多图像预览删除不起作用

来自分类Dev

jQuery图像预览更改

来自分类Dev

jQuery图像预览错误

来自分类Dev

使用PHP从数据库上传,预览,删除图像和文档

来自分类Dev

使用jQuery裁剪上传预览

来自分类Dev

如何删除jQuery文件上传中的fakepath?

来自分类Dev

拖放图像输入文件并在上传之前进行预览

来自分类Dev

在yii框架中上传多个文件,在保存图像之前显示预览

来自分类Dev

jQuery通过ajax上传和预览图像onchange,而无需提交整个表单

来自分类Dev

在以HTML上传之前如何预览图像?

来自分类Dev

在React中上传之前获取图像预览

来自分类Dev

在Opencart的产品页面中预览上传的图像

来自分类Dev

在可拖动的内部预览上传的图像

来自分类Dev

可视化图像上传预览