本文介绍了如何使用带有数据属性的Rate Yo jQuery星级插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何使用 Rate Yo 带有数据属性的jQuery星级插件?
是否可以通过HTML属性设置默认初始值?
是否可以使用data-rateyo-score传递score值.

How can I use Rate Yo jQuery star rating plugin with data attribute?
Is there any way to set a default initial value via HTML attribute?
Is it possible using data-rateyo-score to pass the score value.

例如,如果我想根据动态值来开始评分,该如何使用回调?

For example, If I want to start my score depending on a dynamic value, How can I use callback for it?

我的代码与以下代码相同:
<div class="rateYo" data-rateyo-score="1"></div>

My code is same as below:
<div class="rateYo" data-rateyo-score="1"></div>

我尝试下面的代码:

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

和这个:

$('.rateYo').rateYo({
  score: $(this).attr('data-rateyo-score');
  }
});

但是似乎出了点问题.

推荐答案

使用"data-rateyo-rating ='1'"代替"data-rateyo-score ='1'"

Use "data-rateyo-rating='1'" instead "data-rateyo-score='1'"

$(function () {
  $(".rateyo").rateYo().on("rateyo.change", function (e, data) {
    var rating = data.rating;
    $(this).parent().find('.score').text('score :'+ $(this).attr('data-rateyo-score'));
    $(this).parent().find('.result').text('rating :'+ rating);
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css" rel="stylesheet"/>
<div>
    <div class="rateyo"
         data-rateyo-rating="1"
         data-rateyo-num-stars="5"
         data-rateyo-score="4"></div>
         <span class='score'>0</span>
         <span class='result'>0</span>
</div>
<hr>
<div>
    <div class="rateyo"
         data-rateyo-rating="2.4"
         data-rateyo-num-stars="5"
         data-rateyo-score="1"></div>
         <span class='score'>0</span>
         <span class='result'>0</span>
</div>
<hr>
<div>
    <div class="rateyo"
         data-rateyo-rating="4"
         data-rateyo-num-stars="5"
         data-rateyo-score="3"></div>
         <span class='score'>0</span>
         <span class='result'>0</span>
</div>

这篇关于如何使用带有数据属性的Rate Yo jQuery星级插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 21:28