![](/img/trans.png)
[英]How to place a button into a select2 result item and handle its click?
[英]Append item in select2 on button click
我在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);
});
但是沒有成功。 當我單擊按鈕時添加了項目,但是當我單擊select2中的項目旁邊的“ X”時,如果我從select2下拉列表中添加項目,則所有項目都將消失,並且從select2下拉列表中僅添加了項目,因此無法刪除它。
有人可以指出我正確的方向嗎? 當我單擊按鈕時,如何在select2中而不是從下拉列表中而是從此列表外部添加新項?
這是我第一次使用select2。 我通過YAML Form Drupal 8模塊使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.