2013-07-02 52 views
1

我試圖從轉換爲select2的輸入的html5數據屬性中讀取一個集合來創建標籤。

這是工作時,我有一個輸入:

$(".tags").select2(
    width: '220px' 
    tags: $(".tags").data('collection') 
) 

但我會喜歡使用元素本身的數據做更安全,我想這一點:

$(".tags").select2(
    width: '220px' 
    tags: $(this).data('collection') 
) 

但失敗並顯示錯誤:

Uncaught query function not defined for Select2 investigador_aplicaciones 

是否知道是否可以使用具有特定選擇的元素本身像$(這個)?

$(".tags").each(function(){ 
    var $this = $(this); 
    $this.select2({ 
    width: '220px', 
    tags: $this.data('collection') 
    }); 
}); 

因爲您的通話this的選擇並不代表元素中:

+0

什麼是select2? – 2013-07-03 12:35:39

+0

@ user182669 - [Select2](http://ivaynberg.github.io/select2/)是一個增強