预览图像并不总是使用 JQuery 和 AJAX Upload jQuery 插件重新加载

发布于 2024-11-27 06:12:08 字数 786 浏览 3 评论 0原文

我有一个可以上传图像的网站,并且我正在使用预览系统来显示所选图像。问题是图像有时会加载到预览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-12-04 06:12:08

考虑到 Math.random() 的可预测性,您确定没有遇到重复的随机值吗? Date.getTime 在这里将是一个更好的解决方案,因为它应该在刷新之间为您提供唯一的标识符。

<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 d = new Date();
                  var ruta = "/../images/ligasDeAmigos/prizes/"+liga+ '?rid=' + d.getTime(); 
                  $('#liga_r2_c3').attr('src',ruta);
            }
        }); 

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.

<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 d = new Date();
                  var ruta = "/../images/ligasDeAmigos/prizes/"+liga+ '?rid=' + d.getTime(); 
                  $('#liga_r2_c3').attr('src',ruta);
            }
        }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文