简体   繁体   English

具有多个输入文件的多个图像预览

[英]Multiple Image preview with multiple input file

How can i preview multiple images uploaded from different inputs? 如何预览从不同输入上传的多个图像? The content from textarea is written inside div seeimg on button press, the textarea contains <img src="$img" id="img1"> ,the id's are from 1 to 15 (img1,img2,img3...img15) and i want the images uploaded by user to be seen inside div seeimg 来自textarea的内容写在div seeimg按钮按下,textarea包含<img src="$img" id="img1"> ,id为1到15(img1,img2,img3 ... img15)和我希望用户上传的图像可以在div seeimg中看到


HTML HTML

<form method="post" enctype="multipart/form-data" id="mainform">
Imagine 1:<br>
 <input type="file" name="img1" id="img1"><br>
     <br><br>     
Imagine 2 :<br>
 <input type="file" name="img2" id="img2"><br>
     <br>
 Imagine 3 :<br>
 <input type="file" name="img3" id="img3"><br>
     <br>
 Imagine 4 :<br>
 <input type="file" name="img4" id="img4"><br>
     <br>
 Imagine 5 :<br>
 <input type="file" name="img5" id="img5"><br>
     <br>
 Imagine 6 :<br>
 <input type="file" name="img6" id="img6"><br>
     <br>
 Imagine 7 :<br>
 <input type="file" name="img7" id="img7"><br>
     <br>
 Imagine 8 :<br>
 <input type="file" name="img8" id="img8"><br>
     <br>
 Imagine 9 :<br>
 <input type="file" name="img9" id="img9"><br>
     <br>
 Imagine 10 :<br>
 <input type="file" name="img10" id="img10"><br>
     <br>
 Imagine 11 :<br>
 <input type="file" name="img11" id="img11"><br>
     <br>
 Imagine 12 :<br>
 <input type="file" name="img12" id="img12"><br>
     <br>
 Imagine 13 :<br>
 <input type="file" name="img13" id="img13"><br>
     <br>
 Imagine 14 :<br>
 <input type="file" name="img14" id="img14"><br>
     <br>
 Imagine 15 :<br>
 <textarea id="insert" name="content"></textarea>
 <input type="file" name="img15" id="img15"><br>
 </form>
 <div id="seeimg">
 </div>

So far the scripts it's showing just the image uploaded on first input file <input type="file" name="img1" id="img1"> ,and if i keep changing the image of the first input it's changing the next instead of the first one. 到目前为止,它只显示上传到第一个输入文件上的图像的脚本<input type="file" name="img1" id="img1"> ,如果我不断改变第一个输入的图像,它会改变下一个而不是第一个。
Jquery jQuery的

   function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            console.log(e.target.result+" , "+i);
            $('#seeimg #img'+i).attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}


$("form#mainform #img"+i).change(function(){
    readURL(this);
    i++;
});

TL;DR the scripts it's working just for the first file input and it keeps changing others img src if i upload another image. TL; DR它正在为第一个文件输入工作的脚本,如果我上传另一个图像,它会不断更改其他img src。

Fiddle: https://jsfiddle.net/eLss3ojx/6/ 小提琴: https //jsfiddle.net/eLss3ojx/6/

I tried hard understand the code ,but i could get that you are having multiple file and you want to show the preview to the user as show as the file is uploaded . 我努力理解代码,但我可以得到你有多个文件,并且你希望在文件上传时向用户显示预览。

The approach is correct,but there are certain basic mistake , you are using the same id for the img and input type file , this will work out, moreover i could couple of more bugs. 方法是正确的,但有一些基本的错误,你使用相同的ID为img和输入类型file ,这将有用,而且我可能会有更多的错误。 To help you to get a over , i have modified you code, which will show preview of different images. 为了帮助您解决问题,我修改了您的代码,它将显示不同图像的预览。

HTML HTML

    <form method="post" enctype="multipart/form-data" id="mainform">
    Imagine 1:<br>
     <input type="file" name="img1" id="img1"><br>
     <img id="preview-img1" />
         <br><br>     
    Imagine 2 :<br>
     <input type="file" name="img2" id="img2"><br>
     <img id="preview-img2" />
         <br>
     Imagine 3 :<br>
     <input type="file" name="img3" id="img3"><br>
     <img id="preview-img3" /> 
         <br>
     Imagine 4 :<br>
     <input type="file" name="img4" id="img4"><br>
     <img id="preview-img4" />     
         <br>
     Imagine 5 :<br>
     <input type="file" name="img5" id="img5"><br>
     <img id="preview-img5" />     
         <br>
     Imagine 6 :<br>
     <input type="file" name="img6" id="img6"><br>
     <img id="preview-img6" />     
         <br>
     Imagine 7 :<br>
     <input type="file" name="img7" id="img7"><br>
     <img id="preview-img7" />     
         <br>
     Imagine 8 :<br>
     <input type="file" name="img8" id="img8"><br>
     <img id="preview-img8" />     
         <br>
     Imagine 9 :<br>
     <input type="file" name="img9" id="img9"><br>
     <img id="preview-img9" />     
         <br>
     Imagine 10 :<br>
     <input type="file" name="img10" id="img10"><br>
     <img id="preview-img10" />     
         <br>
     Imagine 11 :<br>
     <input type="file" name="img11" id="img11"><br>
     <img id="preview-img11" />     
         <br>
     Imagine 12 :<br>
     <input type="file" name="img12" id="img12"><br>
     <img id="preview-img12" />     
         <br>
     Imagine 13 :<br>
     <input type="file" name="img13" id="img13"><br>
     <img id="preview-img13" />     
         <br>
     Imagine 14 :<br>
     <input type="file" name="img14" id="img14"><br>
     <img id="preview-img14" />     
         <br>
     Imagine 15 :<br>
     <textarea id="insert" name="content"></textarea>
     <input type="file" name="img15" id="img15"><br>
      <img id="preview-img15" />    
     </form>

Javascript 使用Javascript

      function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                imgId = '#preview-'+$(input).attr('id');
                $(imgId).attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
      }


      $("form#mainform input[type='file']").change(function(){
        readURL(this);
      });

In your JSfiddle you set src not to an image, but to a div. 在你的JSfiddle中,你将src设置为不是图像,而是设置为div。 You have to create an image element and append it to your div like so: 您必须创建一个图像元素并将其附加到您的div,如下所示:

$('<img>').attr('id', '#pre_img' + i).attr('src', e.target.result).appendTo('#vezi');

instead of 代替

$(principdiv+'#img'+i).attr('src', e.target.result);

Also, your code binds the change handlers incorrectly. 此外,您的代码错误地绑定了change处理程序。 You should do something like this instead: 你应该做这样的事情:

$("#mainform input[id^='img']").change(function() {
  readURL(this);
}); 

JSFiddle 的jsfiddle

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

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