简体   繁体   English

Jquery 隐藏和删除 append 后的图像并上传

[英]Jquery hide and delete image after append and upload

I am trying to hide and delete image, this is the append code after upload我正在尝试隐藏和删除图像,这是上传后的 append 代码

Append code Append代码

 $('#uploaded_images').append('<div class="container uploaded_image"> <input type="text" value="'+data['result']+'" name="uploaded_image_name[]" id="uploaded_image_name" hidden> <img class="img-thumbnail" width="200" height="200" src="server/uploads/'+data['result']+'" /> <a  id="delete" href="index.php?image='+data['result']+'" class="btn btn-danger">'+data['result']+'</a> </div>');

Hide code隐藏代码

$('body').on('click', '#delete', function() {
   // code here
   $(this).hide();
});

For some reason $(this).hide();出于某种原因$(this).hide(); it's not working它不工作

I also would like to send a GET request to delete.php?image=+data['result']+ to delete the image from files.我还想向delete.php?image=+data['result']+发送 GET 请求以从文件中删除图像。

Does somebody have a solution?有人有解决方案吗?

because your code is only delete button is hide.first find parent div and after this parent div hide this is proper working.因为您的代码只是删除按钮是 hide.first 找到父 div 并在此父 div 隐藏后这是正常工作。 Replace this替换这个

$(this).hide();

to

 $(this).parent('.uploaded_image').hide();

Here is working fiddle for hide / remove: https://jsfiddle.net/usmanmunir/wj2rLhus/5/这是隐藏/删除的工作小提琴: https://jsfiddle.net/usmanmunir/wj2rLhus/5/

$('body').on('click', '#delete', function() {
   $(this).parent().remove();
});

To Remove files from the server upload directory you can send delete.php?image=fileName要从服务器上传目录中删除文件,您可以发送delete.php?image=fileName

In delele.php在 deele.php

$FileName = $_GET['image']

Store file name as you want by using the function implode in PHP通过使用 PHP 中的 function 内爆来根据需要存储文件名

$FileName = array('image1', 'image2', 'image3');
$Dash_Added = implode("-", $array);

echo $Dash_Added;

unlink() will let you delete file from your upload directory if thats what you want.如果那是您想要的,unlink() 将允许您从上传目录中删除文件。

unlink('server/uploads'.$FileName)

Upload file and store them to array上传文件并将它们存储到数组

<input type="file" onchange="SelectFiles(this)" multiple/>
<input type="submit" onclick="UploadFiles(this)" value="Upload"/>


<script>

var allFileName = []

function SelectFiles(_this) {
  for (var i = 0; i < _this.files.length; i++) {
   allFileName.push(_this.files[i]);
  }
}

function UploadFiles() {
$.ajax({                 
  url:'haha.php',                 
  dataType:'json',                 
  type:'POST',                 
  cache:true,                 
  data: {                   
    file_names: allFileName                 
  },
  success: function(response) {

    }
  })
}

</script>

Hope this helps.希望这可以帮助。

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

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