检查Dropzone是否已连接

Man*_*ion 4 jquery dropzone.js

页面上有很少的dropzones,新的项目由ajax加载,所以我需要检查,如果dropzone已经附加在该项目上.

    Dropzone.autoDiscover = false;


    function initDropzones()
    {

        $('.dropzones').each(function () {

            // how to check dropzone exists on item?
            // or how to destroy already existed dropzone (so reinitialize after)

            $(this).dropzone({
                url: ...
            })
        });
    }

    someAjaxAdd() {
        // add new elements and with new one dropzone
        initDropzones();
    }
Run Code Online (Sandbox Code Playgroud)

非常感谢你

小智 6

你必须检查dropzone属性,如果它存在,你可以销毁它:

function initDropzones() {
    $('.dropzone').each(function () {

        let dropzoneControl = $(this)[0].dropzone;
        if (dropzoneControl) {
            dropzoneControl.destroy();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)