let count = 0; // Track how many copies we have let $template = $(‘.c-input–select’); // The HTML you want to copy each time let $container = $(‘.for_select’); // Where the copies should be added $(‘#add_select’).on(‘click’, function() { // Increment our counter count++; // Create a copy of your base HTML/select let $copy = $template.clone(); // Find the select in the div, and give it an id so we can find it $copy.find(‘select’).attr(‘id’, count); // Append it $container.append($copy); // Initialise it as a select2, using the id we just gave it to find it $(‘#’ + count).select2(); });.hidden { display: none;}
注意事项
确保在初始化 Select2 之前,相关的 HTML 元素已经添加到 DOM 中。每次动态创建 Select2 控件后,都要立即对其进行初始化。使用唯一的 ID 可以避免多个 Select2 控件之间的冲突。根据实际需求,可以调整 Select2 的配置选项。
使用go run -race检测数据竞争,结合runtime.NumGoroutine监控协程数量,通过pprof分析阻塞调用栈,利用select超时避免永久阻塞,有效排查goroutine泄漏、死锁和数据竞争问题。 Go语言的goroutine和channel是并发编程的核心,但它们也带来了调试上…