我正在处理从我的图库中删除图片的功能。在PHP方面,它的工作原理。使用Jquery / Ajax也可以。但是当我修改我的jquery以删除图像时删除图像(删除元素客户端)它不再起作用(当我说不再有效时它会给我代码中内置的错误函数:
此代码有效:
function deleteImage(file_name)
{
var r = confirm("Are you sure you want to delete this Image?")
if(r == true)
{
$.ajax({
method: "POST",
url: '/images/gallery/deleteImage.php',
data: {'delete_file' : file_name },
success: function (response) {
ohSnap("Image has been deleted.", "green");
},
error: function () {
ohSnap("An error has occured.", "red");
}
});
}
}
但是这个没有
function deleteImage(file_name)
{
var r = confirm("Are you sure you want to delete this Image?")
if(r == true)
{
$.ajax({
method: "POST",
url: '/images/gallery/deleteImage.php',
data: {'delete_file' : file_name },
success: function (response) {
$('#' + file_name).remove();
ohSnap("Image has been deleted.", "green");
},
error: function () {
ohSnap("An error has occured.", "red");
}
});
}
}
知道为什么添加remove函数导致错误?在控制台中,它并没有告诉我原因,所以我输了。
更新:
以下是其中一个元素的示例:
<div class="thumbImage" id="happy_anime_s2-1433126427.png">
<a href="images/gallery/happy_anime_s2-1433126427.png" data-featherlight="image">
<img src="images/gallery/happy_anime_s2-1433126427.png" width="200px" alt="" />
</a>
<span class="deleteImage">
<input type="hidden" value="happy_anime_s2-1433126427.png" name="delete_file" id="delete_file" />
<input type="button" value="Delete image" onclick="deleteImage('happy_anime_s2-1433126427.png');"/>
</span>
</div>
答案 0 :(得分:2)
问题是id中的.
,它创建了一个类选择器。查看选择器#happy_anime_s2-1433126427.png
,它会查找标识为happy_anime_s2-1433126427
且类png
的元素。
您需要使用.
\\.
$('#' + file_name.replace('.', '\\.')).remove();
使用任何元字符(例如 !“#$%&amp;'()* +,。/:;&lt; =&gt;?@ [] ^`{|}〜)作为名称的字面部分,必须 用两个反斜杠逃脱:\。