如何在 select 新评级值时删除以前的 selected 选项

How to remove previous selected option when select new rating value

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

我想在用户 select 新评分值时删除之前的 selected 选项。 这是脚本:

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

这是 select 选项的输出方式,每次用户 select 新评分值

您的代码完美运行:

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