预览图像并不总是使用 JQuery 和 AJAX Upload jQuery 插件重新加载
我有一个可以上传图像的网站,并且我正在使用预览系统来显示所选图像。问题是图像有时会加载到预览 div,但其他时候它保持不变。但是,如果我点击刷新按钮,那么图像实际上会发生变化。因此,控制器操作肯定会被调用,它只是并不总是刷新预览图像。这是我的代码:
<img name="liga_r2_c3" src="/../images/ligasDeAmigos/prizes/<?php print $model->prize_photo?>" width="100" height="100" border="0" id="liga_r2_c3" alt=""/>
new AjaxUpload('liga_r2_c3', {
action: 'uploadImage',
data: {
liga_id : liga,
isPrize : 'true',
},
onComplete: function(file, response) {
var liga = $("#hidden_prize_picture").val();
var ruta = "/../images/ligasDeAmigos/prizes/"+liga+ '?rid=' + Math.random();
$('#liga_r2_c3').attr('src',ruta);
}
});
I have a site where I can upload an image, and i am using a preview system where i show the selected image. The problem is that the image SOMETIMES is loaded to the preview div, but other times it remains the same. However, if i hit the refresh button, then the image does in fact change. Therefore, the controller action is definitely getting called, it just doesnt always refresh the preview image. This is my code:
<img name="liga_r2_c3" src="/../images/ligasDeAmigos/prizes/<?php print $model->prize_photo?>" width="100" height="100" border="0" id="liga_r2_c3" alt=""/>
new AjaxUpload('liga_r2_c3', {
action: 'uploadImage',
data: {
liga_id : liga,
isPrize : 'true',
},
onComplete: function(file, response) {
var liga = $("#hidden_prize_picture").val();
var ruta = "/../images/ligasDeAmigos/prizes/"+liga+ '?rid=' + Math.random();
$('#liga_r2_c3').attr('src',ruta);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑到 Math.random() 的可预测性,您确定没有遇到重复的随机值吗? Date.getTime 在这里将是一个更好的解决方案,因为它应该在刷新之间为您提供唯一的标识符。
Considering the predictability of Math.random() are you sure you are not running into duplicate random values? Date.getTime would be a much better solution here since it should give you a unique identifier between refreshes.