简体   繁体   English

无法在“MutationObserver”上执行“观察”:参数 1 不是“节点”类型。 选择2()

[英]Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. select2()

I try to use select2 for dynamic data received via ajax, in response I get the error that I wrote above.我尝试将 select2 用于通过 ajax 接收的动态数据,作为响应,我收到了上面写的错误。 What am I doing wrong?我究竟做错了什么? here is code:这是代码:

$(document).on('change', '[name="country"]', function () {
    var vehicle_id = $(this).val();
    $.ajax({
        type: 'GET',
        url: '{{ route('select_vehicle') }}',
        data: { vehicle_id: vehicle_id },
        success: function (data) {
            $('.dyn').html(data);
        }
    });
    //  $('.dyn').on('DOMNodeInserted', 'select', function () {$(this).select2();});
    var target = $('.dyn'); //elm = $('.select2');
    const config = { childList: true };
    if (target) {
        let observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (mutation && mutation.addedNodes) {
                    mutation.addedNodes.forEach(function (elm) {
                        if (elm && elm.nodeName === "select") { $(elm).select2(); }
                    });
                }
            });
        });
        observer.observe(target, config);
    }
});

Thanks for your attention.感谢您的关注。

I solved the problem.I put in dynamic results new div with id #inits.It works.Thanks for your attention.我解决了这个问题。我输入了 ID 为 #inits 的动态结果新 div。它有效。感谢您的关注。

 $('#inits select.select2').select2();}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在“MutationObserver”上执行“观察”:参数 1 不是“节点”类型 - Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node' 未捕获的类型错误:无法在“MutationObserver”上执行“观察”:参数 1 的类型不是“节点” - Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node' “MutationObserver”上的“观察”:参数 1 不是“节点”类型 - 'observe' on 'MutationObserver': parameter 1 is not of type 'Node' 我收到“未捕获的类型错误:无法在‘MutationObserver’上执行‘观察’:参数 1 不是‘节点’类型。” - I am getting "Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'." Javascript-无法在“MutationObserver”上执行“观察” - Javascript- Failed to execute 'observe' on 'MutationObserver' 无法在“IntersectionObserver”上执行“observe”:参数 1 不是“元素”类型 - Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element' 未捕获的类型错误:无法在“IntersectionObserver”上执行“观察”:参数 1 不是“元素”类型 - Uncaught TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element' 无法在“节点”上执行“removeChild”:参数 1 的类型不是“节点” - Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node' 无法在“节点”上执行“ insertBefore”:参数1不是“节点”类型 - Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node' 无法在'Node'上执行'appendChild':参数1不是'Node'类型 - Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM