Select2 - 从循环中获取数据

时间:2017-10-31 09:58:47

标签: jquery jquery-select2 jquery-select2-4

我有一个表格表,每行包含一个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

                    });

1 个答案:

答案 0 :(得分:4)

您必须在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');