【发布时间】:2021-08-10 10:18:53
【问题描述】:
I want following conditions in my code basically i post filefield from backend and i want to display my filefield with respective sources:
1. When i post image, My video and audio sources should be automatically hide.
2. When i post video, My image and audio sources should be automatically hide.
3. When i post audio, My video and image sources should be automatically hide.
My jquery code: In this code my else if part is not working.I am also using template literals in that code.
// 读取或检索博客数据(此函数调用创建、更新和删除成功消息)
read_retrieve_blogs()
function read_retrieve_blogs() {
var card = document.getElementById('card');
card.innerHTML = ''
$.ajax({
url: "http://localhost:8000/user_blogs/",
cache: false,
success: function(data) {
var list = data
for (var i in list) {
var item = `
<img src="${list[i].image}" class="card-img-top" id="r-image" height="280" width="280" alt="...">
<video class="card-img-top" width='400' id="r-video" controls>
<source src="${list[i].image}" type='video/mp4'>
</video>
<audio class="card-img-top" id="r-audio" controls width="320" height="240">
<source src="${list[i].image}" type="audio/ogg">
</audio>
<div class="card-body" id="card-body">
<h5 class="card-title">${list[i].title}</h5>
<h5 class="card-title">Written by ${list[i].author}</h5>
<h5 class="card-title">on ${list[i].date} | ${list[i].time}</h5>
<p class="card-text">${list[i].description}</p>
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group me-2" role="group" aria-label="First group">
<button type="button" data-id="${list[i].id}" class="btn btn-success"
data-bs-toggle="modal" data-bs-target="#updateModal">Update</button>
</div>
<div class="btn-group me-2" role="group" aria-label="Second group">
<input type="button" data-sid="${list[i].id}" class="btn btn-danger" value="Delete"
onclick="return confirm('Are You Sure For Delete?')">
</div>
</div>
`
card.innerHTML += item
if ($('#r-image').is('[src$=".png"],[src$=".jpeg"],[src$=".jpg"]')) {
$('#r-image:not([src=""])').show();
$('#r-video:not([src=""])').hide();
$('#r-audio:not([src=""])').hide();
} else if ($('#r-video').is('[src$=".mp4"]')) {
$('#r-image:not([src=""])').hide();
$('#r-video:not([src=""])').show();
$('#r-audio:not([src=""])').hide();
}
}
}
});
}
I want following conditions in my code:
1. When i post image, My video and audio sources should be automatically hide.
2. When i post video, My image and audio sources should be automatically hide.
3. When i post audio, My video and image sources should be automatically hide.
我希望在我的代码中包含以下条件: 1.当我发布图片时,我的视频和音频源应该会自动隐藏。 2.当我发布视频时,我的图像和音频源应该会自动隐藏。 3.当我发布音频时,我的视频和图像源应该会自动隐藏。
【问题讨论】:
-
一个 jQuery 对象总是真实的。如果您想知道它是否匹配任何内容,请测试长度。
-
您还需要详细说明您要如何处理这些
src属性 -
现在,我更新了我的问题。请再次查看。
-
并非如此。损坏的代码不能很好地替代书面解释。准确告诉我们您想要完成的代码尝试
标签: javascript html jquery ajax coding-style