英文:
JavaScript returns value on the first review only, l don't know why
问题
It seems like you're having an issue where only the stars for the first review are displayed when looping through Django reviews and using JavaScript. You want to display stars for each review. Here's the translated code:
Html
{% for review in reviews %}
<p>显示数字</p>
<option value="{{ review.rating }}" id="hours"></option>
<div class="staz">
<p id="demo"></p>
</div>
{% endfor %}
JavaScript
<script>
let hours = document.querySelectorAll('#hours');
let stazElements = document.querySelectorAll('.staz');
for (let i = 0; i < hours.length; i++) {
let hour = hours[i].value;
let greetings;
if (hour == 1){
greetings = "★";
}
else if (hour == 2){
greetings = "★★";
}
else if (hour == 3){
greetings = "★★★";
}
else if (hour == 4){
greetings = "★★★★";
}
else if (hour == 5){
greetings = "★★★★★";
}
stazElements[i].querySelector('#demo').innerHTML = greetings;
}
</script>
This updated code should display stars for each review, not just the first one.
英文:
Am having a problem but l nor know what brings it.
When I try to loop the Django to for JavaScript to display the Stars, it returns only on the first review and the rest it returns nothing.
Now what can't do so that each user who adds a rating it displays it's stars not displaying only the first one?
Below it's my HTML and JavaScript below
Even l added also an image for results if try to reflesh the page.
Html
{% for review in reviews %}
<p>display of numbers </p>
<option value="{{ review.rating }}" id="hours"></option>
<div class="staz">
<p id="demo"></p>
{% endfor %}
JavaScript
<script>
let hour = document.getElementById('hours').value;
let greetings;
if (hour == 1){
greetings = "★";
}
else if (hour == 2){
greetings = "★★"
}
else if(hour == 3){
greetings = "★★★"
}
else if(hour == 4){
greetings = "★★★★"
}
else if(hour == 5){
greetings = "★★★★★";
}
document.getElementById('demo').innerHTML = greetings
</script>
Results
I expected that each review, it returns it's stars.
答案1
得分: 0
将您的 id
更改为 class
,然后使用 querySelectorAll
获取所有具有 hours
类的元素并将所需的星星分配给 div。
因此,您的 django
代码将更改为:
{% for review in reviews %}
<!--...一些代码在此处添加类-->
<option value="{{ review.rating }}" class="hours"></option>
<div class="staz">
<p class="demo"></p> <!--在此处添加类-->
{% endfor %}
演示代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const hours_div = document.querySelectorAll('.hours');
hours_div.forEach(function(item) {
var hour = item.value;
var greetings = ''
if (hour == 1) {
greetings = "★";
} else if (hour == 2) {
greetings = "★★";
} else if (hour == 3) {
greetings = "★★★";
} else if (hour == 4) {
greetings = "★★★★";
} else if (hour == 5) {
greetings = "★★★★★";
}
item.nextElementSibling.children[0].innerHTML = greetings
})
<!-- language: lang-html -->
<p>display of numbers </p>
<option value="4" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="3" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="2" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="1" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<!-- end snippet -->
英文:
You can change your id
to class
then using querySelectorAll
get all element which has hours
class and assign required star to div .
So, your django
code will be change to :
{% for review in reviews %}
<!--...some codes added class here -->
<option value="{{ review.rating }}" class="hours"></option>
<div class="staz">
<p class="demo"></p> <!--added class here -->
{% endfor %}
Demo Code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const hours_div = document.querySelectorAll('.hours');
hours_div.forEach(function(item) {
var hour = item.value;
var greetings = ''
if (hour == 1) {
greetings = "★";
} else if (hour == 2) {
greetings = "★★"
} else if (hour == 3) {
greetings = "★★★"
} else if (hour == 4) {
greetings = "★★★★"
} else if (hour == 5) {
greetings = "★★★★★";
}
item.nextElementSibling.children[0].innerHTML = greetings
})
<!-- language: lang-html -->
<p>display of numbers </p>
<option value="4" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="3" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="2" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<p>display of numbers </p>
<option value="1" class="hours"></option>
<div class="staz">
<p class="demo"></p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论