raty评级多重评级的默认值

时间:2014-04-13 20:03:35

标签: php jquery raty

我正在使用jquery raty(评级脚本)。我有多个评级选项,如果没有选择它们,则生成的隐藏帖子字段不存在。这就是我能够为所有4个选项命名并拉出所选分数的方法。

<section>
<label>Option 1:</label>
<div class="options" data-score-name="option1"></div>
</section>

<section>
<label>Option 2:</label>
<div class="options" data-score-name="option2"></div>
</section>

<section>
<label>Option 3:</label>
<div class="options" data-score-name="option3"></div>
</section>

<section>
<label>Option 4:</label>
<div class="options" data-score-name="option4"></div>
</section>


<script type="text/javascript">
$('.options').raty({
  half: true,
  width: 'width: 150',
  path: '/assets/img',
  starOff: 'star-off.png',
  starOn: 'star-on.png',
  starHalf: 'star-half.png',
  readOnly: false,
  scoreName: 'score[]',

  click: function(score)
  {
     var ScoreName = $(this).attr('data-score-name');
     $(this).append("<input type='hidden' name='rate[" + ScoreName + "]' value='" + score + "'>");
  }

});
</script>

我遇到的问题是,如果未选中,我如何使每个选项都有默认值?我不确定是否有一个onload选项会给它一个默认值,并且只能使用onclick。

1 个答案:

答案 0 :(得分:0)

想出来,就像一个魅力。这可能是错误的做法,但它有效:)

<script type="text/javascript">
$('.options').raty({
  half: true,
  width: 'width: 150',
  path: '/assets/img',
  starOff: 'star-off.png',
  starOn: 'star-on.png',
  starHalf: 'star-half.png',
  readOnly: false,
  scoreName: 'score[]',
  score: function() {
    var ScoreName = $(this).attr('data-score-name');
    return $(this).append("<input type='hidden' name='rate[" + ScoreName + "]' class='" + ScoreName + "' value='0'>");
  },



  click: function(score)
  {
     var ScoreName = $(this).attr('data-score-name');
     $("." + ScoreName).val(score);
     //$(this).append("<input type='hidden' name='rate[" + ScoreName + "]' value='" + score + "'>");
  }

});
</script>