Select2在添加动态表单后消失。

huangapple go评论60阅读模式
英文:

Select2 disappearing, after adding dynamic form

问题

问题是,每次单击"addNewItemBtn"按钮时,先前字段的"select2"函数会消失。在加载页面时,"select2"仍然有效。但在单击"addNewItemBtn"按钮后,您可能会注意到"select2"函数在添加新字段后消失。您如何使即使在添加新字段后,先前字段的状态仍然保持不变?

解决方案:

要解决这个问题,您可以在将新字段添加到表格后,重新初始化"select2",以便新字段也具有"select2"功能。这是您的代码示例的修改版本:

$(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 = `
            <!-- Your new table row content -->
        `;
        $('tbody').append(productItem);

        // Reinitialize select2 for all relevant elements
        $('#product_type_id, #product_id, #unit_of_measure_id').select2();

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

通过在添加新字段后重新初始化"select2",您可以确保新字段也具有相同的功能,而不会影响先前字段的状态。

英文:

i have a problem regarding select2, what happen is I have a dynamic form with few select2, but everytime I click the addNewItembtn the previous fields' select2 function will disappear.

here's the code:

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

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

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

Upon loading the page, the select2 is still working
first load

After clicking the add new item button
enter image description here

You may notice that the select2 function did disappear after adding new fields. How can I make it that even after adding new fields the state of the previous page will remain. TIA

How can I make it that even after adding new fields the state of the previous page will remain. TIA

答案1

得分: 1

以下是已翻译的内容:

这里出现了一个问题,原因是id=&quot;product_type_id&quot;id=&quot;product_id&quot;ID 属性应该是唯一的,所以最好在创建行时创建动态的ID,或者干脆从代码中删除ID属性。换句话说,目前不需要这个。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
$(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="">-- 产品类型 --</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="">-- 产品 --</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]" class="form-control serial_number">
                </td>
                <td>
                    <input type="text" name="productItems[${productItems}][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]" class="form-control quantity" value="1">
                </td>
                <td>
                    <input type="number" name="productItems[${productItems}][item_cost]" class="form-control item_cost" value="0.00">
                </td>
                <td>
                    <a href="#" class="deleteProductItemBtn">删除</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();
    }
});
<!-- 语言:lang-html -->
<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">添加项目</button>
<table>
    <tbody></tbody>
</table>
<!-- 结束代码片段 -->
英文:

Here is issue happening due to id=&quot;product_type_id&quot; id=&quot;product_id&quot;. The ID property should be unique so it's better to create dynamic ID's with row creation or simply remove the ID attribute from code. In other words you cannot need that for now.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js&quot;&gt;&lt;/script&gt;
&lt;button id=&quot;addProductItemBtn&quot;&gt;Add Item&lt;/button&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 10:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定