首页 > 解决方案 > Bootstrap-star-rating 插件 - 事件被多次调用

问题描述

我正在使用这个带有 JQuery 3.1.1 的Bootstap 星级插件版本 v4.0.5。我在调用 livequery 插件(来自作者Brandon Aaron)之后初始化了插件,因为我的星级输入是在 DOM 已经初始化时按需添加的。整个事情都很好,唯一的例外是事件被调用了这么多次。当用户与之交互时,它们在同一元素上被触发的次数不断增加。 (见下图)

请问,这可能是什么原因?有什么修复吗?

我的脚本:

$('.langLevel').livequery(function() {
  $('#langLevel0').rating({
    step: 1,
    min: 0,
    max: 5,
    size: 'sm',
    theme: 'krajee-fa',
    // clearButton: '',
    captionElement: "",
    showCaptionAsTitle: false,
    starCaptions: {
      1: 'Beginner',
      2: 'Intermediate',
      3: 'Advanced',
      4: 'Proficient',
      5: 'Native'
    },
    starCaptionClasses: {
      1: 'text-danger',
      2: 'text-warning',
      3: 'text-info',
      4: 'text-primary',
      5: 'text-success'
    }
  }).on("rating:clear", function(event) {
    console.info("Your rating is reset");
  }).on("rating:change", function(event, value, caption) {
    console.info("You rated: " + value + " = " + $(caption).text());
  });
}, function() {
  // unmatchFn
  return;
});

预先感谢您的帮助

标签: javascriptjquerylivequery

解决方案


推荐阅读