选择新的评级值时如何删除先前选择的选项

时间:2019-07-18 11:09:53

标签: javascript jquery rating

我正在使用jquery-bar-rating https://github.com/antennaio/jquery-bar-rating

当用户选择新的评分值时,我想删除以前选择的选项。 这是脚本:

<script>
    $(function () {
        $('.rating').barrating('show',{
            theme: 'fontawesome-stars',
            initialRating: '0',
            onSelect: function (value, text, event) {
                if (typeof (event) !== 'undefined') {
                  $('#avis_nombre option:selected').removeAttr('selected');
                  document.querySelector('#avis_nombre option[value="' + value + '"]').setAttribute('selected', 'selected');
                }
            },
        });
    });
</script>

这是每次用户选择新的评级值时输出选择选项的方式

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码运行完美:

$(function() {
  $('#example').barrating('show', {
    theme: 'fontawesome-stars',
    initialRating: '0',
    onSelect: function(value, text, event) {
      if (typeof(event) !== 'undefined') {
        $('#avis_nombre option:selected').removeAttr('selected');
        document.querySelector('#avis_nombre option[value="' + value + '"]').setAttribute('selected', 'selected');
      }
    },
  });
});
<link href="https://antennaio.github.io/jquery-bar-rating/dist/themes/fontawesome-stars.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<script src="https://antennaio.github.io/jquery-bar-rating/dist/jquery.barrating.min.js"></script>

<select id="example">
  <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>

<select id="avis_nombre">
  <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>