2016-11-21 269 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模块使用它。

回答