2016-09-06 64 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