JavaScript 只在第一次调用时返回值,我不知道为什么。

huangapple go评论59阅读模式
英文:

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 %}
   &lt;p&gt;display of numbers &lt;/p&gt;
   &lt;option value=&quot;{{ review.rating }}&quot; id=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
&lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;
{% endfor %}

JavaScript

&lt;script&gt;
    let hour = document.getElementById(&#39;hours&#39;).value;
    let greetings;
        if (hour == 1){
            greetings = &quot;★&quot;;
        }
        else if (hour == 2){
            greetings = &quot;★★&quot;
        }
        else if(hour == 3){
            greetings = &quot;★★★&quot;
        }
        else if(hour == 4){
            greetings = &quot;★★★★&quot;
        }
        else if(hour == 5){
            greetings = &quot;★★★★★&quot;;
        }
        document.getElementById(&#39;demo&#39;).innerHTML = greetings
&lt;/script&gt;

Results

JavaScript 只在第一次调用时返回值,我不知道为什么。

I expected that each review, it returns it's stars.

答案1

得分: 0

将您的 id 更改为 class,然后使用 querySelectorAll 获取所有具有 hours 类的元素并将所需的星星分配给 div。

因此,您的 django 代码将更改为:

{% for review in reviews %}
  &lt;!--...一些代码在此处添加类--&gt;
  &lt;option value=&quot;{{ review.rating }}&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
  &lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt; &lt;!--在此处添加类--&gt;
{% endfor %}

演示代码

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

const hours_div = document.querySelectorAll(&#39;.hours&#39;);
hours_div.forEach(function(item) {
  var hour = item.value;
  var greetings = &#39;&#39;
  if (hour == 1) {
    greetings = &quot;★&quot;;
  } else if (hour == 2) {
    greetings = &quot;★★&quot;;
  } else if (hour == 3) {
    greetings = &quot;★★★&quot;;
  } else if (hour == 4) {
    greetings = &quot;★★★★&quot;;
  } else if (hour == 5) {
    greetings = &quot;★★★★★&quot;;
  }
  item.nextElementSibling.children[0].innerHTML = greetings
})

&lt;!-- language: lang-html --&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;4&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;3&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;2&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;1&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;!-- end snippet --&gt;
英文:

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 %}
 &lt;!--...some codes added class here --&gt;
   &lt;option value=&quot;{{ review.rating }}&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
&lt;p class=&quot;demo&quot;&gt;&lt;/p&gt; &lt;!--added class here --&gt;
{% endfor %}

Demo Code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const hours_div = document.querySelectorAll(&#39;.hours&#39;);
hours_div.forEach(function(item) {
  var hour = item.value;
  var greetings = &#39;&#39;
  if (hour == 1) {
    greetings = &quot;★&quot;;
  } else if (hour == 2) {
    greetings = &quot;★★&quot;
  } else if (hour == 3) {
    greetings = &quot;★★★&quot;
  } else if (hour == 4) {
    greetings = &quot;★★★★&quot;
  } else if (hour == 5) {
    greetings = &quot;★★★★★&quot;;
  }
  item.nextElementSibling.children[0].innerHTML = greetings
})

<!-- language: lang-html -->

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;4&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;3&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;2&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;display of numbers &lt;/p&gt;
&lt;option value=&quot;1&quot; class=&quot;hours&quot;&gt;&lt;/option&gt;
&lt;div class=&quot;staz&quot;&gt;
  &lt;p class=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月19日 23:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定