jquery - 无法使用 select2 正确克隆表行

标签 jquery jquery-select2

我有一个发票表格,我需要根据需要添加项目。单击相应行上的“添加”按钮后,该行已附加到上一行。但此后 Select2 无法正常工作。

我尝试过 SO 的解决方案,其中在克隆之前销毁选择,然后再次初始化它。但它在第一行之后初始化两个 select2。

<tr class="tr_clone">
    <td>
        <div class="input select">
            <select required="required" id="Item" class="items select2-hidden-accessible" name="data[Invoice][item_id][]" tabindex="-1" aria-hidden="true">
                <option value=""></option>
                <option value="1">item 1</option>
                <option value="2">Item 2</option>
                <option value="3">Item 1+2</option>
                <option value="4">Set 1</option>
                <option value="5">NURSERY Books</option>
            </select><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 231px;"><span class="selection"><span aria-expanded="false" aria-haspopup="true" aria-autocomplete="list" role="combobox" class="select2-selection select2-selection--single" tabindex="0" aria-labelledby="select2-Item-container"><span class="select2-selection__rendered" id="select2-Item-container" title=""></span><span role="presentation" class="select2-selection__arrow"><b role="presentation"></b></span></span>
            </span><span aria-hidden="true" class="dropdown-wrapper"></span></span>
        </div>
        <input type="hidden" id="ItemName" class="item_name" name="data[Invoice][item][]">
        <input type="hidden" id="ItemName" class="item_group_id" name="data[Invoice][item_group_id][]"> </td>
    <td>
        <div class="input text">
            <input type="number" required="" min="0" name="data[InvoiceDetail][quantity][]" class="quantity small" id="quantity][">
            <p class="quantity_a"></p>
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="" name="data[InvoiceDetail][price][]" class="price small" id="price">
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="" name="data[InvoiceDetail][unit][]" class="unit small" id="unit][">
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="required" id="Amount" class="amount small" name="data[Invoice][amount][]">
        </div>
    </td>
    <td>
        <input type="button" class="tr_clone_add" value="Add" name="add">
    </td>
    <td class="remove">X</td>
</tr>

Jquery 代码:

$("table").on('click','.tr_clone_add' ,function() {
        $('.items').select2("destroy");        
        var $tr = $(this).closest('.tr_clone');
        var $clone = $tr.clone();
        $clone.find(':text').val('');
        $tr.after($clone);      
        $('.items').select2();      
    });

结果:

enter image description here

最佳答案

试试这个

$('.items').select2();
$("table").on('click','.tr_clone_add' ,function() {
   $('.items').select2("destroy");        
   var $tr = $(this).closest('.tr_clone');
   var $clone = $tr.clone();
   $tr.after($clone);
   $('.items').select2();
   $clone.find('.items').select2('val', '');
});

JSFIDDLE example

/* 编辑*/

如果我从 SELECT 属性 id 和 css 类 select2-hidden-accessible 中删除,并在底部完全删除 select2 select2-container 它似乎工作正常。

JSFIDDLE example with your row

关于jquery - 无法使用 select2 正确克隆表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178154/

相关文章:

jquery - ASP.NET WebForms/jQuery : jQuery . 按钮上的单击事件使按钮的服务器端 OnClick 事件无法触发

Javascript效果动态填充瓶子中的水位

javascript - 无法转到模态轮播中的另一张图片

javascript - 如果选中,则禁用第二个选择框的选项

javascript - select2 (v4.0.0) - 链式选择不起作用?

javascript - 在 html 中存储外部 php 样式表的内容

javascript - jQuery Select2 : [object Window] in tags value

javascript - 重置 Ajax 表单提交上的 Select2 下拉菜单

javascript - 防止 select2 的标签文本中出现空格

javascript - 过渡保持结构上的光滑 slider