我在一个项目中有一个图像上传器,并使用Ajax上传图像,这是非常完美的。它显示上传的图像立即不刷新页面。下面是我用来上传图片的代码:
<script>
$(function () {
$('#btnUpload').click(function () {
var fileUpload = $("#FileUpload1").get(0);
var files = fileUpload.files;
var test = new FormData();
for (var i = 0; i < files.length; i++) {
test.append(files[i].name, files[i]);
}
$.ajax({
url: "../UI/Upload.ashx",
type: "POST",
contentType: false,
processData: false,
data: test,
success: function (result) {
alert(result);
//This section refreshes the div with uploaded images and shows images without full page refresh
$('#divImages').load(document.URL + ' #divImages');
},
error: function (err) {
alert(err.statusText);
}
});
});
});
</script>
<input type="file" id="FileUpload1" />
<input type="button" id="btnUpload" value="Upload Files" />
<div id="divImages" clientidmode="Static" runat="server">
<asp:Label ID="labelImages" ClientIDMode="Static" runat="server"></asp:Label>
</div>问题是上传图像后,图像会显示在内容中,但无法单击图像,并且“Delete”链接与似乎也被阻塞的每个图像相关联。然后,当我刷新整个页面时,点击图片和链接就可以了。我不知道为什么会发生这种事?在浏览器的检查元素中,我可以看到新创建的div,如下所示:
<div id="divImages"> //The newly created div after partial refresh with Ajax every time I upload image
<div id="divImages" clientidmode="Static" runat="server">
<asp:Label ID="labelImages" ClientIDMode="Static" runat="server"></asp:Label>
</div>
</div>它是否阻止我点击图片/按钮或其他任何东西?如果有人指出的话,我将不胜感激。
这是我用来删除带有链接的图像的代码(基本上我使用链接作为按钮):
$('#divImages a.deleteLink').click(function () { //Ajax used to delete images from 'Images' folder with jQuery
var image = $(this).attr("img");
$.ajax({
type: "POST",
url: "../UI/DeleteImage.ashx",
data: "imageName=" + image,
contentType: 'application/x-www-form-urlencoded',
success: function (response) {
if (response == "true") {
$('#divImages a.imageLink[imgsrc*=\"' + image + '\"]').fadeOut();
$('#divImages a.deleteLink[img=\"' + image + '\"]').fadeOut();
}
},
error: function (response) {
alert('There was an error. ' + response);
}
});
});
});发布于 2016-07-14 09:20:11
“行动”要求我把这个作为回答,但我什么也做不了。
请参阅我在评论部分的解释。
$('body').on('click', '#divImages a.deleteLink', function() {https://stackoverflow.com/questions/38368134
复制相似问题