2

我正在使用 jquery 星级插件“Raty” https://github.com/wbotelhos/raty。我正在使用 PHP 从数据库中生成结果集。这也包括分数。我想为每个单独的评分组件设置 score 属性。我怎样才能做到这一点?我正在使用这种语法来显示星星。

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

<div class="stars_small"></div>

单个页面中有许多 div,类“stars_small”是动态生成的。我想为每个 div 设置“分数”。

4

5 回答 5

6
$('.stars_small').raty({
             readOnly : true,
             half  : true,
             score: function() {
                      return $(this).attr('data-rating');
                     }
              space : false
         });  

这对我来说很好。该插件正在像这样添加一个隐藏的div文本class="stars_small"

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

所以我只是value用来自数据库查询的数字设置属性。

于 2012-12-07T07:27:03.173 回答
0

试试这个

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

         $(this).raty({
                    readOnly : true,
                    half  : true,
                    score : 2,
                    space : false
              }); 
        });
于 2012-09-28T04:35:16.047 回答
0

假设您已经使用 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]);

    });

});
于 2012-09-28T04:44:26.197 回答
0

这是我所做的:

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

<div class="star s0"></div>
<div class="star s1"></div>
<div class="star s2"></div>

我还在我的模板中生成值数组(即,将值从我的服务器端脚本传递到网页)。像这样:

var array = new Array(2.4, 2.9, 5.0);

然后我在调用中声明所有三个“星标”的共同点$(".star"),并在一个循环中设置值:

$('.star').raty({
    half : true
});

for (i = 0; i < array.length; i++) { $('.s' + i).raty({ score:array[i] }); }

于 2012-11-23T15:37:22.387 回答
0

对于 V.2.7 Jquery,比率为以下形式:

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

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

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

$('div').raty({
    score: function() {
    return $(this).attr('data-score');
  }
});
于 2015-04-14T23:08:20.877 回答