我想在上传图片后立即显示。为此,我在 ajax 的 .success()
函数中添加了函数 $('#imageInput').on('change', function())
但它确实不行。 alert("good")
弹出“good”,但 alert("better")
不会弹出“better”。
有什么想法可以实现吗?
JS:
function afterSuccess(data)
{
if(data.indexOf('error')<0)
{ alert ("good");
$('#imageInput').on('change', function() {
alert ("better");
readPath(this);
});
}
}
最佳答案
$('#imageInput').on('change', function() {...});
事件不会触发,因为在选择文件时上传字段发生变化 (预上传)。上传文件时该字段不会更改。
更新:关于你的后续问题(见下面的评论),考虑这样的事情,其中 data
数组可能会有所不同,具体取决于你的 php 脚本返回的内容和插入方法可能会因您的 HTML 布局而异(在这种情况下,我假设您要插入一个元素:
function afterSuccess(data)
{
if(data.indexOf('error')<0)
{
var filepath = data['filepath'];
$("#imageContainer").html("<img src="+filepath+"/>");
} else {
alert('Sorry, but an error occurred while uploading the image. Please try again');
}
}
关于javascript - 上传后立即显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001117/