[英]Jquery hide and delete image after append and upload
我正在嘗試隱藏和刪除圖像,這是上傳后的 append 代碼
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>');
隱藏代碼
$('body').on('click', '#delete', function() {
// code here
$(this).hide();
});
出於某種原因$(this).hide();
它不工作
我還想向delete.php?image=+data['result']+發送 GET 請求以從文件中刪除圖像。
有人有解決方案嗎?
因為您的代碼只是刪除按鈕是 hide.first 找到父 div 並在此父 div 隱藏后這是正常工作。 替換這個
$(this).hide();
至
$(this).parent('.uploaded_image').hide();
這是隱藏/刪除的工作小提琴: https://jsfiddle.net/usmanmunir/wj2rLhus/5/
$('body').on('click', '#delete', function() {
$(this).parent().remove();
});
要從服務器上傳目錄中刪除文件,您可以發送delete.php?image=fileName
在 deele.php
$FileName = $_GET['image']
通過使用 PHP 中的 function 內爆來根據需要存儲文件名
$FileName = array('image1', 'image2', 'image3');
$Dash_Added = implode("-", $array);
echo $Dash_Added;
如果那是您想要的,unlink() 將允許您從上傳目錄中刪除文件。
unlink('server/uploads'.$FileName)
上傳文件並將它們存儲到數組
<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>
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.