我有一个关于selt2的问题,发生的情况是我有一个动态表单,但每次我单击addNewItembtn之前字段的elect2函数时,它就会消失.

代码如下:

$(document).ready(function() {
            // set dropdown to select2
            $('#warehouse_id').select2();
            $('.product_id').select2();
            $('.unit_of_measure_id').select2();
            $('.product_type_id').select2();

            $('#addProductItemBtn').click(function(e) {
                e.preventDefault();
                let productItems = $('tbody tr').length;
                let productItem = `
                    <tr>
                        <td>
                            <select name="productItems[${productItems}][product_type_id]" id="product_type_id" class="form-control product_type_id">
                                <option value="">-- Product Type --</option>
                                @foreach ($productTypes as $productType)
                                    <option value="{{ $productType->id }}"
                                        @selected(old("productItems.{$loop->index}.product_type_id"))>
                                        {{ $productType->title }}
                                    </option>
                                @endforeach
                            </select>
                        </td>
                        <td>
                            <select name="productItems[${productItems}][product_id]" id="product_id" class="form-control product_id">
                                <option value="">-- Product --</option>
                                @foreach ($products as $product)
                                    <option value="{{ $product->id }}"
                                        @selected(old("productItems.{$loop->index}.product_id"))>
                                        {{ $product->product_name }}
                                    </option>
                                @endforeach
                            </select>
                        </td>
                        <td>
                            <input type="text" name="productItems[${productItems}][serial_number]" id="serial_number" class="form-control serial_number">
                        </td>
                        <td>
                            <input type="brand" name="productItems[${productItems}][brand]" id="brand" class="form-control brand">
                        </td>
                        <td>
                            <select name="productItems[${productItems}][unit_of_measure_id]" id="unit_of_measure_id" class="form-control unit_of_measure_id">
                                <option value="">-- UOM --</option>
                            </select>
                        </td>
                        <td>
                            <input type="number" name="productItems[${productItems}][quantity]" id="quantity" class="form-control quantity" value="1">
                        </td>
                        <td>
                            <input type="number" name="productItems[${productItems}][item_cost]" id="item_cost" class="form-control item_cost" value="0.00">
                        </td>
                        <td>
                            <a href="#" class="deleteProductItemBtn">Delete</a>
                        </td>
                    </tr>
                `;
                $('tbody').append(productItem);
                $('.product_id').select2();
                $('.unit_of_measure_id').select2();
                $('.product_type_id').select2();
            });

            $(document).on('click', '.deleteProductItemBtn', function(e) {
                e.preventDefault();
                $(this).closest('tr').remove();
            });
});

加载页面后, Select 器2仍在工作 first load

在单击添加新项目按钮后 enter image description here

您可能注意到,在添加新字段后,selt2函数确实消失了.如何才能使添加新字段后上一页的状态保持不变.提亚

如何才能使添加新字段后上一页的状态保持不变.提亚

推荐答案

这是由于id="product_type_id" id="product_id"发生的问题.ID属性应该是唯一的,因此最好使用行创建来创建动态ID,或者简单地从代码中删除ID属性.换句话说,你目前还不需要它.

$(document).ready(function () {
    initSelect2();
    $('#addProductItemBtn').click(function (e) {
        e.preventDefault();
        let productItems = $('tbody tr').length;
        let productItem = `
                <tr>
                    <td>
                        <select name="productItems[${productItems}][product_type_id]" class="form-control product_type_id">
                            <option value="">-- Product Type --</option>
                            @foreach ($productTypes as $productType)
                                <option value="{{ $productType->id }}"
                                    @selected(old("productItems.{$loop->index}.product_type_id"))>
                                    {{ $productType->title }}
                                </option>
                            @endforeach
                        </select>
                    </td>
                    <td>
                        <select name="productItems[${productItems}][product_id]" class="form-control product_id">
                            <option value="">-- Product --</option>
                            @foreach ($products as $product)
                                <option value="{{ $product->id }}"
                                    @selected(old("productItems.{$loop->index}.product_id"))>
                                    {{ $product->product_name }}
                                </option>
                            @endforeach
                        </select>
                    </td>
                    <td>
                        <input type="text" name="productItems[${productItems}][serial_number]" id="serial_number" class="form-control serial_number">
                    </td>
                    <td>
                        <input type="brand" name="productItems[${productItems}][brand]" id="brand" class="form-control brand">
                    </td>
                    <td>
                        <select name="productItems[${productItems}][unit_of_measure_id]" class="form-control unit_of_measure_id">
                            <option value="">-- UOM --</option>
                        </select>
                    </td>
                    <td>
                        <input type="number" name="productItems[${productItems}][quantity]" id="quantity" class="form-control quantity" value="1">
                    </td>
                    <td>
                        <input type="number" name="productItems[${productItems}][item_cost]" id="item_cost" class="form-control item_cost" value="0.00">
                    </td>
                    <td>
                        <a href="#" class="deleteProductItemBtn">Delete</a>
                    </td>
                </tr>
            `;
        $('tbody').append(productItem);

        initSelect2();
    });

    $(document).on('click', '.deleteProductItemBtn', function (e) {
        e.preventDefault();
        $(this).closest('tr').remove();
    });

    function initSelect2() {
        $('#warehouse_id').select2();
        $('.product_id').select2();
        $('.unit_of_measure_id').select2();
        $('.product_type_id').select2();
    }
});
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<button id="addProductItemBtn">Add Item</button>
<table>
    <tbody></tbody>
</table>

Jquery相关问答推荐

确定元素是否是其父元素的最后一个子元素

如何在 jQuery 中使用:not 和 hasClass() 来获取没有类的特定元素

为什么对同一个 ASP.NET MVC 操作的多个同时 AJAX 调用会导致浏览器阻塞?

如何用 javascript/jquery 替换 url 参数?

在 select2 中使用 AJAX 进行标记

jQuery: Select 属性大于值的所有元素

删除所有子元素的 CLASS

如何使用jQuery动态设置宽度和高度

jQuery $.cookie 不是一个函数

将返回的 JSON 对象属性转换为(较低的第一个)camelCase

Javascript中的选定文本事件触发器

jquery变量语法

我可以使用 jQuery 打开下拉列表吗

获取元素的底部和右侧位置

如何将文本从 div 复制到剪贴板

如何使用 jQuery 在悬停时显示工具提示消息?

Isotope 和 Masonry jQuery 插件之间的区别

Select jQuery UI 自动完成后清除表单字段

如何使用 JQuery 删除onclick?

jQuery 将换行符转换为 br (nl2br 等效)