【发布时间】:2020-06-13 19:27:28
【问题描述】:
我正在尝试使用具有两个选项的下拉菜单创建个人资料图片编辑器:
更新您的头像
如果有任何个人资料图片,它将显示删除选项,否则它将隐藏此选项
模板代码:
<div class="dropdown-menu">
<a class="dropdown-item"><input name="input_file" type="file" id="profile-pic-upload" style="display: none;" />
<label for="profile-pic-upload">Upload Profile Picture</label>
</a>
{% if user_profile.profile_pic %}
<a class="dropdown-item delete" href="javascript:" style="font-size : 14px;">Delete</a>
{% endif %}
</div>
JS代码:
$(".delete").click(function () {
$('.loading').show();
var url = "{% url 'user-profile-image-delete' %}";
$.ajax({
type: 'POST',
url: url,
data: $("#new_form").serialize(),
cache: false,
success: function (data, status) {
$('#imagePreview').css('background-image', 'url('+'{% static 'assets/img/user-64x.png' %}'+')');
if (data['status'] == "success") {
}
else {
toastr.error(data.msg);
}
}
});
});
$("#profile-pic-upload").change(function () {
var input_detail = this;
var data = new FormData();
var file = this.files[0];
data.append("file", file);
$.ajax({
type: 'POST',
data: data,
contentType: false,
processData: false,
url: $("#profile-file-upload").data('action'),
cache: false,
success: function (data, status) {
if (data['status'] == true) {
toastr.success(data.msg);
var randomId = new Date().getTime();
$('#imagePreview').css('background-image', 'url(' + data.profile_pic + '?random=' + randomId + ')');
}
else {
toastr.error(data.msg);
}
}
});
});
但我面临一个问题:即使我点击 delete 选项,下拉菜单也不会刷新,它仍然显示删除它的选项。只有在我刷新页面时才会出现。
同样的方式,如果没有个人资料图片,它只会显示上传个人资料图片。如果我更新图像,下拉菜单不会显示删除选项。要看到这一点,我需要刷新页面。
如何避免页面重新加载刷新下拉内容?
【问题讨论】:
-
您可以尝试将 false 值初始化为 user_profile.profile_pic 。一旦图像被成功删除。喜欢:{% with user_profile.profile_pic=false %}
-
我们如何才能在 ajax 中添加这个成功???
-
它适用于普通 HTML 以及 javascript 试用版。
标签: javascript jquery django django-templates