php - Jquery 图像源更改不起作用
问题描述
我正在尝试通过用新的图像文件覆盖现有图像文件来通过 ajax 更改用户图像,那么如何在不重新加载页面的情况下显示用户图像。
$(document).ready(function (e) {
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
// $("#message").empty();
// $('#loading').show();
alert('i m clicked');
$.ajax({
url: "ajax_php_file.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
// $('#loading').hide();
$('#previewing').hide();
// $("#message").html(data);
// $('PreviewImage').attr("src","<?php echo $ImageSource; ?>");
var img = $("#PreviewImage");
img.attr("src","<?php echo $ImageSource; ?>");
// alert(data);
}
});
}));
解决方案
根据@Deepak 评论,我已将timestamp
与图像连接起来source
以显示新图像。
如果您的图像没有刷新,请在图像 url 之后添加时间字符串,例如
$timestamp = time(); img.attr("src","<?php echo $ImageSource.'?'.$timestamp;");
推荐阅读
- angularjs - Angular 10 KendoUI 访问 ng-template 外的 dataItem
- c++ - 奇怪的 operator[] 行为
- reactjs - 操作和状态显示在控制台上,但在带有钩子的应用程序上未定义
- search - TYPO3 ke_search 用于手风琴、卡片等的自定义索引器(来自 bootstrap_package 的所有元素)?
- c++ - C++ 故障检测二维数组中的冲突
- elasticsearch - Elasticsearch 为每个用户获取最新的徽章值
- python - 如何测试由保存的异常引起的循环?
- python - Python3 - 更改传入变量的引用
- amazon-web-services - 如何通过向 Lambda 函数发送 API 请求来安排在特定时间启动 EC2 实例?
- reactjs - 反应日期选择器 - 删除周