Рейтинговый рейтинг не отображается должным образом

Я использую плагин raty для отображения звездочек рейтинга на нескольких предметах. Я инициализирую рейтинг по атрибуту данных, как

 <span class="rating_score" data-rating="{{  review.rating}}" ></span>

затем я бегу по классам, и я начинаю raty, как следует

jQuery(".rating_score").each(function() {
                            $_this = jQuery(this);
                            console.log(($_this.attr('data-rating') ));
                            jQuery(this).raty({
                                path: '/bundles/gfx/rating/',
                                starOn: 'star.gif',
                                starOff: 'star_empty.gif',
                                hintList: ['1', '2', '3', '4', '5'],
                                scoreName: 'rating',
                                start: ($_this.attr('data-rating') ),
                                width: 13,
                                readOnly: true
                            });
                        });

проблема в том, что отображаемое значение рейтинга будет одинаковым для каждого элемента, что неправильно во фрагменте

1 ответ

Решение

Попробуйте это (см. Комментарий, что я изменил):

jQuery(".rating_score").each(function() {
  $_this = jQuery(this);
  jQuery(this).raty({
    starOn: 'star',                      // this should be a class name with your class styling the star image rather than a gif
    starOff: 'star_empty',               // this should be a class name with your class styling the empty star image rather than a gif
    hintList: ['1', '2', '3', '4', '5'],
    scoreName: 'rating',
    score: $_this.attr('data-rating'),  // instead of start, use score and remove the brackets
    readOnly: true                      // remove this if you want to be able to select a new rating
  });
});
.rating_scrore {
  display: block;
}

.star,
.star_empty {
  display: inline-block;
  width: 15px;
  height: 15px; /* change width & height to match star gif dimensions*/
}

.star {
  background: red; /*change this to be you star.gif*/
}

.star_empty {
  background: blue; /*change this to be you star_empty.gif*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jacob87.github.io/raty-fa/lib/jquery.raty-fa.js"></script>
<span class="rating_score" data-rating="1"></span><br />
<span class="rating_score" data-rating="2"></span><br />
<span class="rating_score" data-rating="3"></span><br />
<span class="rating_score" data-rating="4"></span><br />

Пример, где вы можете выбрать новый рейтинг

Другие вопросы по тегам