2016-09-06 34 views
0

我使用http://antenna.io/demo/jquery-bar-rating/examples/为表中的人员设置多个评分。一切正常,但我不能 选择的数据属性的相应值传递到评级功能,因此它保存正确的人的评级。jQuery barrating.js - 发送ID数据

$('.rating').each(function(index, value) { 
    var dataId = $(this).closest('select').attr("data-rating-candidate"); 
    //console.log(dataId); 

    $('.rating').barrating('show', { 
     theme: 'bars-1to10', 
     onSelect: function(rating, text, event) { 
      if (typeof(event) !== 'undefined') { 
       console.log(dataId); 
      } 
     } 
    }); 
}); 

显然,只有最后一次传递的属性值用于dataId变量。那么,如何才能为选定的评分获得正确的评分?我似乎无法在barrating选项中使用$ this?

回答

2

您不能使用$ this。但是,相反,barlet在onSelect回调中有一个名为'this'的变量。例如,我的元素具有'data-shop-id'属性,然后通过执行此操作可以在onSelect中访问它的值:

onSelect: function(value, text, event) { 
       var el   = this; 
       var shop_id  = el.$elem.data('shop-id'); 
//do your stuff here     
} 
+0

谢谢,这有帮助! – Chris