带有select2的jQuery转发器无法正常工作

问题描述

我要从2天开始寻找这个问题。

一个元素运行正常。但是在重复该元素之后,上一个元素的select2字段会自动销毁。

转发器中有两个rop div。当ajax请求选择第一个选项时,我会将选项附加到values部分。

这是供参考的屏幕截图。

enter image description here

这是转发器和select2初始化的代码

<script>
        $(function () {

            $(".attribute_list").on('change',".attribute",function (e) {
                var attr_name = $(this).attr('name');
                var attr_id = $(this).val();
                $.ajax({
                    type: "POST",url: "{{ route('products.getValue') }}",headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },contentType: "application/json",dataType: "json",data: '{"attr_id": "' + attr_id + '","attr_name" : "' + attr_name + '"}',success: function (data) {
                        var attribute_value = data.attribute_value;
                        var field_name = 'select[name="product_attribute[' + data.field_name + '][attribute_values][]"]';

                        if (attribute_value) {
                            $(field_name).empty();
                            $.each(attribute_value,function (key,value) {
                                $(field_name).append('<option value="' + value.id + '">' + value.value + '</option>');
                            });
                            // $(field_name).select2();

                        } else {
                            $(field_name).empty();
                        }
                    }
                });
            });
        });

    </script>
    <script>
        // $("#attribute_button").click(function(){
        //     $('#attribute_values').select2({
        //                 placeholder: "select attribute value"
        //             });
        // });
        window.onload = function () {
            $('.attribute_values').select2({
                placeholder: "select attribute value",allowClear: true
            });
        }

        $('.repeater').repeater({

            initEmpty: false,show: function () {
                $(this).slideDown();
                $('.attribute_values').select2({
                    placeholder: "select attribute value"
                });
            },hide: function (deleteElement) {
                if (confirm('Are you sure you want to delete this element?')) {
                    $(this).slideUp(deleteElement);
                }
            },ready: function (setIndexes) {
            },})

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)