克隆 <select> 失败,出现“.select2 不是函数”错误

Failed to clone <select> with ".select2 is not a function" error

我正在尝试以动态形式使用 Select2(使用 jQuery 克隆添加新行),但显然克隆的行不属于 Select2 的功能。收到错误

第二个 select 菜单已创建但产生错误:

Uncaught TypeError: $(...).select2 is not a function

我正在使用这个 HTML 部分:

<tbody id="treatmentlist">
    <tr class="itemData">
        <td>
           {{Form::select('treatments[]',$treatments, null, ['class' => 'form-control treatment', 'placeholder' => 'Select treatment' ])}}
        </td>
        <td><i class="fas fa-plus-circle" id='addRow'><i class="fas fa-minus-circle" id="delRow"></i></td>
    </tr>
</tbody>

这是脚本部分:

@section('scripts')

    <script>
        $(".treatment").select2();
        $("#addRow").on('click', function (e) {
            e.preventDefault();
            var $tr    = $(this).closest('.itemData');
            var $clone = $tr.clone();
            $tr.after($clone);
            $(".treatment").select2();
        });
    </script>

@endsection

已经尝试添加 select2('destroy') 但没有成功。

关于如何使新克隆的行保留 Select2 功能的任何建议?

我无法在 50 声望以下发表评论,希望这个示例对您有所帮助。 解释在这里 http://www.phpjavascript.com/2018_03_16_archive.html

$(function () {
    $("#addrow").click(function () {
        var tr = "<tr>" + "<td contenteditable='true'></td>".
                repeat($("#myTable tr:eq(0) td").length) + "</tr>"
        $("#myTable").append(tr);
    });
    $("#addcolumn").click(function () {
        $("#myTable tr").append($("<td contenteditable='true'></td>"));
        $(document).on('click', '.js-del-row', function () {
            $('#myTable').find('tr:last').remove();
        });
        $(document).on('click', '.js-del-column', function () {
            $('#myTable').find('td:last').remove();
        });
    });
});

显然该问题与 Laravel 布局页面 (app.blade.php) 有关。调用 bootstrap 时它已经加载 jQuery。所以在 php 中我不需要再次加载 jQuery。

因此 app.blade.php 的修订:

    <!-- Scripts, remove defer from app.js, move selec2.min.js & select2.min.css here -->
    <script src="{{ asset('js/app.js') }}"></script>  
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

...
...

<!- and adding this part -->  
@yield('scripts')

在 php 端,Select2 的变化是:

  1. 在 onClick 函数之后,先调用 select2('destroy')
  2. 然后克隆包含 select 元素的行
  3. 最后使用 select2() 在最后一个新添加的元素上重新启用 Select2。
@section('scripts')
<script>

$(document).ready(function () {
    $("#contents").children("td").children("select").select2();

    $(document).on('click', '#delRow', function(){
        if( $(this).closest('tr').is('tr:only-child') ) {
            alert('cannot delete last row');
        }
        else {
            $(this).closest('tr').remove();
        }

    });

    $("#addRow").click(function () {
        $("#contents").children("td").children("select").select2("destroy").end();

        // add <tr><td> directive first
        var newRow = '<tr id="contents"><td id="trx"><td> <i class="fas fa-minus-circle" id="delRow">';
        $(newRow).appendTo($("#treatmentlist"));

        // append select component on the last tr
        $('#treatmentlist tr:last').children("td:first-child").append(
            // clone the row and insert it in the DOM
            $("#contents").children("td").children("select").first().clone()
        );
        // enable Select2 on the last newly added element
        $('#treatmentlist tr:last').children("td").children("select").select2();
        // enable Select2 on the select elements
        $("#contents").children("td").children("select").select2();
    });
});
</script>
@endsection

在 HTML 中应用以下更改:

                <tbody id="treatmentlist">
                    <tr id="contents">
                        <td id="trx">
                            {{Form::select('treatments[]',$treatments, null, ['class' => 'form-control treatment', 'placeholder' => 'Select treatment' ])}}
                        </td>
                        <td>
                            <i class="fas fa-plus-circle" id='addRow'></td>
                        </td>
                    </tr>
                </tbody>