Select2 - 从循环中获取数据

Select2 - Get data from a loop

我有一个表单-table,每行包含一个 select2,我想从每个表单中获取数据并将其存储到数组中。下面是我将使用的代码,不确定它是否正确尝试。

// event on each form change re-get the values
         $("#main").bind("keyup change", function (e) {


                        $('#form > tbody  > tr').each(function (i) {

        // get the data for each select
        // add it to array and use after loop for some logic
        // not sure if need this event here : select2:select
        // also the selector might be not in the node of current tr?

                            $('.selector').on("select2:select", function (e) {

                                var data = $(this).select2('data');

                               // push to array ?? data[0].custom_data

                            });

                        });

    // logic from array would be here

                    });

您必须在 tr

中找到所需的元素

替换

$('.selector').on("select2:select", function (e) {

                            var data = $(this).select2('data');

                           // push to array ?? data[0].custom_data

                        });

var data = $(this).find('.selector').select2('data');