0

我试图弄清楚如何获得所需的 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'
    });
4

1 回答 1

1

因为你定义了:

scoreName: 'pixrating'

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

我的例子和小提琴

$(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>

于 2016-08-24T19:00:09.450 回答