2016-11-21 121 views
0

我在Drupal YAML表單中使用select2標籤樣式,因此用戶可以找到並添加一些項目。在select2上添加項按鈕單擊

所有這些項目,我與圖像和標題在同一頁上,並在每個圖像旁邊有按鈕。我想讓用戶點擊按鈕並在select2中添加該項目(就像使用select2下拉列表一樣)。

我想是這樣的:

$('a.rentiraj').click(function (e) { 
       e.preventDefault(); 
       // Get title 
       var masina = $(this).closest('.views-row').find('.views-field-title span').text(); 
       // Result to add in select2 ul 
       var result = '<li class="select2-selection__choice" title="' + masina + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + masina + '</li>'; 
       // Add result in select2 ul before last li 
       $('.select2-selection ul li:last-child').before(result); 
      }); 

但沒有成功。當點擊按鈕時會添加項目,但如果在select2中的項目之外單擊「X」,則無法將其刪除,如果從select2下拉列表中添加項目,則所有項目都會消失,並且只會從select2下拉列表中添加項目。

有人能指點我正確的方向嗎?如何在select2中添加新項目,而不是從下拉列表中添加新項目,但是當我單擊按鈕時,從列表外部添加新項目?

這是我第一次使用select2。我通過YAML Form Drupal 8模塊使用它。

回答