【问题标题】:Jquery Raty start plugin : set score for multiple divJquery Raty 启动插件:为多个 div 设置分数
【发布时间】:2012-09-28 04:27:54
【问题描述】:

我正在使用 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 设置“分数”。

【问题讨论】:

    标签: php jquery raty


    【解决方案1】:
    $('.stars_small').raty({
                 readOnly : true,
                 half  : true,
                 score: function() {
                          return $(this).attr('data-rating');
                         }
                  space : false
             });  
    

    这对我来说很好。 该插件在每个divclass="stars_small" 中添加一个隐藏的文本框 像这样

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

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

    【讨论】:

    • 优雅的解决方案。 Raty 是一个不错的图书馆!
    • score: function() { return $(this).attr('data-rating'); }后加逗号
    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        这是我所做的:

        我为每个星形 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

        【讨论】:

          【解决方案5】:

          对于 V.2.7 的 Jquery 比率是以下形式:

          如果你需要根据一个动态值来开始你的得分,你可以使用回调。

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

          <div data-score="1"></div>
          
          $('div').raty({
              score: function() {
              return $(this).attr('data-score');
            }
          });
          

          【讨论】:

            猜你喜欢
            • 2015-09-08
            • 1970-01-01
            • 2017-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-25
            • 1970-01-01
            相关资源
            最近更新 更多