简体   繁体   English

img src无法使用JS / JQuery正常工作:显示上一张图片而不是新上传的图片

[英]img src not working as expected using JS/JQuery: previous image get displayed instead of new uploaded image

I have a form to upload images. 我有一张上传图片的表格。 By default the source is "images/noimage.png" ; 默认情况下,源为"images/noimage.png" ;

<img id="previewing" src="images/noimage.png" />

I store the path in the database. 我将路径存储在数据库中。 If I select a specific user and the form gets loaded I want to display the image for the specific user. 如果我选择一个特定用户并且表单被加载,我想显示该特定用户的图像。 I do this in the windows.onload event. 我在windows.onload事件中执行此操作。 However another image (previous image the user had, if user had one) gets loaded instead of the new one. 但是,将加载另一个图像(如果用户有一个,则为用户先前的图像),而不是新的图像。

var Logo = 'Logos/123.png'; //path stored in Database

if (Logo!='') {
  //If the user has a 'Logo', show it
   $("#previewing").prop("src", Logo);
} else { 
  // if user has no 'Logo', show default image
   $("#previewing").prop("src", 'Images/noimage.png');
}

This does not work as expected. 这不能按预期方式工作。 What's wrong here? 怎么了

If after reloading image getting change then most probably its Caching issue. 如果重新加载映像后发生更改,则很可能是其缓存问题。 you may try adding some random string into image URL like 您可以尝试在图像网址中添加一些随机字符串,例如

Logos/123.png?v=xyz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM