Jquery Raty开始插件:为多个div设置得分

I am using jquery star rating plugin "Raty" https://github.com/wbotelhos/raty. i am generating a result set from database with PHP. that includes score also. I want set score property for every individual rating component. how can I do that? I am using this syntax for showing the stars.

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

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

there are many div in a single page with the class "stars_small" generated dynamically. I want to set "score" for every div.

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

this worked fine for me. the plugin is adding a hidden textbox in every div with class="stars_small" like this

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

So I just set the value attribute with number coming from database query.

Try this

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

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

Assuming that you have generated with PHP the JS lines:

// 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...

You can run you rating star with :

$(document).ready(function() {

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

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

    });

});

Here is what i did:

I created a separate class for each of those star divs:

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

I also generate the array of values in my template (that is, pass the values from my server-side script to the web page). Like so:

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

And then I declare the thing common for all the three "star banners" in the $(".star") call, and set the values in a cycle:

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

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

For V.2.7 Jquery raty is the form:

If you need to start you score depending of a dynamic value, you can to use callback for it.

You can pass any value for it, not necessarily a data- value. You can use a field value for example.

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

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