Php Jquery Raty start插件:为多个div设置分数

Php Jquery Raty start插件:为多个div设置分数,php,jquery,raty,Php,Jquery,Raty,我正在使用jquery星级插件“Raty”。 我正在用PHP从数据库生成一个结果集。这也包括分数。我想为每个单独的评级组件设置分数属性。我该怎么做? 我使用这种语法来显示星星 $('.stars_small').raty({ readOnly : true, half : true, score : 2, space : false }); <div class="stars_small"></div> $('.

我正在使用jquery星级插件“Raty”。 我正在用PHP从数据库生成一个结果集。这也包括分数。我想为每个单独的评级组件设置分数属性。我该怎么做? 我使用这种语法来显示星星

$('.stars_small').raty({
      readOnly : true,
      half  : true,
      score : 2,
      space : false
 });  

<div class="stars_small"></div>
$('.stars\u small').raty({
只读:对,
一半:是的,
得分:2分,
空格:false
});  
单个页面中有许多div,动态生成类“stars\u small”。我想为每个分区设置“分数”。

试试这个

   $('.stars_small').each(function() {

         $(this).raty({
                    readOnly : true,
                    half  : true,
                    score : 2,
                    space : false
              }); 
        });

假设您已使用PHP生成JS行:

// lines of JS generated with a loop in PHP (for the rate content)
var rates = [];
rates.push({...one rate coming from PHP...});
rates.push({...one rate coming from PHP...});
// etc...
您可以使用以下工具运行您的评级之星:

$(document).ready(function() {

    $(".starts_small").each(function(index, element) {

        $(this).raty(rates[index]);

    });

});
以下是我所做的:

我为每个星星div创建了一个单独的类:

这对我很管用。 该插件使用
class=“stars\u small”
像这样

<div class="stars_small">
     <input type="hidden" name="score" value="3.5" readonly="readonly">
</div>


因此,我只需使用来自数据库查询的数字设置
属性

对于V.2.7 Jquery raty,其形式如下:

如果需要根据动态值开始评分,则可以使用回调

您可以为它传递任何值,不一定是数据值。例如,可以使用字段值

<div data-score="1"></div>

$('div').raty({
    score: function() {
    return $(this).attr('data-score');
  }
});

$('div').raty({
分数:函数(){
返回$(this.attr('data-score');
}
});

优雅的解决方案。拉蒂是一个很好的图书馆!在
score:function(){return$(this).attr('data-rating');}
$('.star').raty({ half : true });

for (i = 0; i < array.length; i++) { $('.s' + i).raty({ score:array[i] }); }
$('.stars_small').raty({
             readOnly : true,
             half  : true,
             score: function() {
                      return $(this).attr('data-rating');
                     }
              space : false
         });  
<div class="stars_small">
     <input type="hidden" name="score" value="3.5" readonly="readonly">
</div>
<div data-score="1"></div>

$('div').raty({
    score: function() {
    return $(this).attr('data-score');
  }
});