Image preview opens for duplicated div

前端 未结 3 1050
小蘑菇
小蘑菇 2021-01-26 00:24

I have a function that uploads and image and adds a preview to this image, and i have two buttons, when i click on the first one it clicks on the input type file and opens it, a

3条回答
  •  离开以前
    2021-01-26 00:43

    Duplication the element and the script together will be helpful
    check this i got it working as you want (took me a good time)

    var i = 0;
    $('#inputfile' + i).change(function() {
      console.log(i);
      if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = imageIsLoaded;
        reader.readAsDataURL(this.files[0]);
      }
    });
    
    
    function imageIsLoaded(e) {
    
      console.log(i);
      $('#ImgUpload' + i).attr('src', e.target.result);
    };
    
    function upload(id) {
      $('#' + id).click();
    };
    
    $('#duplicate').click(function() {
      i++;
      var newelement =
    
        '
    ' + '' + '' + 'Your Image' + '

    ' + '
    Your Image

提交回复
热议问题