Select2 v4 clone不传递数据

我正在尝试使用Select2 ver4 jquery插件和使用Select2示例页面的加载远程数据进行AJAX调用。 我正在尝试克隆包含select2工具的选择。 问题克隆之前选择已经由优秀的顾问工作!!谢谢!

但克隆元素不起作用使用新的AJAX(test.php)。

HTML代码

      
Get befor select value.

jQuery代码

 $.fn.select2.amd.require( ["select2/core", "select2/utils", "select2/compat/matcher"], function (Select2, Utils, oldMatcher) { var $ajax = $(".js-example-data-ajax"); $ajax.select2({ ajax: { url: "https://api.github.com/search/repositories", dataType: 'json', delay: 250, data: function (params) { return { q: params.term // search term }; }, processResults: function (data, params) { params.page = params.page || 1; return { results: data }; }, cache: true }, escapeMarkup: function (markup) { return markup; }, minimumInputLength: 1, }); }); $(document).on('click', '.addline', function () { var $tr = $(this).closest('tr'); var $lastTr = $tr.closest('table').find('tr:last'); $lastTr.find('.js-example-data-ajax').select2('destroy'); var $clone = $lastTr.clone(true); $clone.find('td').each(function() { var el = $(this).find(':first-child'); var id = el.attr('id') || null; if (id) { var i = id.substr(id.length - 1); var prefix = id.substr(0, (id.length - 1)); el.attr('id', prefix + (+i + 1)); el.attr('name', prefix + (+i + 1)); } }); $tr.closest('tbody').append($clone); $(".js-example-data-ajax").select2({ ajax: { url: "https://api.github.com/search/repositories", dataType: 'json', delay: 250, data: function (params) { return { q: params.term // search term }; }, processResults: function (data, params) { params.page = params.page || 1; return { results: data }; }, cache: true }, escapeMarkup: function (markup) { return markup; }, minimumInputLength: 1, }); // Don't work code from here $('.js-example-data-ajax').on("change",function() { $.get ('test.php', {da : $(this).val()}, function (data) { $('.hint').html(data); }); }); }); // Don't work code from here $(document).ready(function(){ $('.js-example-data-ajax').on("change",function() { $.get ('test.php', {da : $(this).val()}, function (data) { $('.hint').html(data); }); }); }); 

test.php代码

 echo $_GET["da"]; 

你在这里要问的是获取属于click事件行目标的.hint

要做到这一点,首先将我们将用于$.get成功的函数定义为全局变量:

 var onSelectGetSuccess = function(element) { return function (data) { // fill in the .hint of the closest tr of our element $(element).closest('tr').find('.hint').html(data); } } 

然后你可以用它来定义像这样的.on("change")事件(注意它出现在代码中的两个地方):

 $('.js-example-data-ajax').on("change",function(e) { $.get ('test.php', {da : $(this).val()}, onSelectGetSuccess(e.target)) });