jquery raty需要评级

时间:2016-08-24 17:58:42

标签: jquery require raty

我正在试图找出如何获得所需的jquery Raty插件。有没有人知道一个简单的方法只是为了确保用户点击了一颗星。如果有人需要看到代码的任何部分,请问我。我对jquery不太满意所以请详细说明谢谢。

    $rates.raty({
        half: false,
        target : '#add_post_rating',
        hints: pixreviews.hints,
        path: path,
        targetKeep : true,
        //targetType : 'score',
        targetType : 'hint',
        //precision  : true,
        score: default_rating,
        scoreName: 'pixrating',
        click: function(rating, evt) {

            $('#add_post_rating' ).val( '' + rating );
            $('#add_post_rating option[value="' + rating + '"]' ).attr(  'selected', 'selected' );

        },
        starType : 'i'
    });

1 个答案:

答案 0 :(得分:1)

因为你定义了:

scoreName: 'pixrating'

Raty将在div中创建一个输入类型隐藏字段。你需要考虑这个领域。

我的示例和fiddle

$(function () {
  $('div').raty({
    half: false,
    hints: ['bad', 'poor', 'regular', 'good', 'gorgeous'],
    path: 'https://raw.githubusercontent.com/wbotelhos/raty/master/lib/images/',
    targetKeep: true,
    //targetType : 'score',
    targetType: 'hint',
    //precision  : true,
    score: 0,
    scoreName: 'pixrating',
    click: function (rating, evt) {
      $('#add_post_rating').val(rating);
    },
    starType: 'i'
  });

  // handle the select box changes
  $('#add_post_rating').on('change', function (e) {
    $('div input[name="pixrating"]').val(this.value);
    $('div').raty('score', this.value);
  }).trigger('change');

  // on form submit do your test....
  $('#frm').on('submit', function(e) {
    if ($('div').raty('score') == '0') {
      e.preventDefault();
      console.log('error: raty is 0!');
      return;
    }
  })
});
<link rel="stylesheet" href="https://rawgit.com/wbotelhos/raty/master/lib/jquery.raty.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://rawgit.com/wbotelhos/raty/master/lib/jquery.raty.js"></script>

<form id="frm">
    <div></div>
    <select id="add_post_rating">
        <option value="0"></option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    <input type="submit" value="Submit">
</form>