按钮上的附加项目中的附加项目

时间:2016-11-21 15:50:54

标签: javascript jquery drupal select2 jquery-select2-4

我在Drupal YAML表单中使用了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);
            });

但没有成功。当我点击按钮时添加了项目,但是当点击“X&#39;”时,我无法将其删除。除了select2中的项目,如果我从select2下拉列表中添加项目,所有项目都会消失,并且只有select2下拉列表中添加了项目。

有人能指出我正确的方向吗?我如何在select2中添加新项目,而不是从下拉列表中添加,但是当我点击按钮时,从列表外部添加新项目?

这是我第一次使用select2。我通过YAML Form Drupal 8模块使用它。

1 个答案:

答案 0 :(得分:0)

从jquery使用ON而不是单击方法:

$('a.rentiraj').on('click', function (e) {

文档:http://api.jquery.com/on/

相关问题