jQuery barrating.js – 发送id数据

jQuery barrating.js – Sending id data

我正在使用 http://antenna.io/demo/jquery-bar-rating/examples/ 为 table 中的人设置多个评分。一切正常,除了我不能 select 的数据属性中的相应值将传递给评分函数,以便为正确的人保存评分。

$('.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 变量。那么,如何获得 selected 评分的正确评分呢?我似乎无法在 barrating selection?

中使用 $this

你不能使用$this。但是 barrating 在 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                
}