【发布时间】:2015-09-07 15:53:44
【问题描述】:
我正在尝试使用 Select2 ver4 jquery 插件和使用 Select2 示例页面的加载远程数据进行 AJAX 调用。 我正在尝试克隆一个包含 select2 工具的选择。 Before Question 克隆选择已经由好的顾问工作了!!谢谢!!
但是使用新的 AJAX(test.php) 克隆元素不起作用。
HTML 代码
<tr>
<td>
<select class="js-example-data-ajax" id="sel1">
</select>
</td>
<td>
<div class="hint">Get befor select value.</div>
</td>
<td>
<button type="button" class="addline">Add Line</button>
</td>
</tr>
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"];
【问题讨论】:
-
您查看了 jQuery 文档并尝试了
clone(true)...? -
@Popnoodles 我看到了你的建议,尝试了克隆(true)。但我选择克隆选择并更改两个 div。我想工作选择只在 div 后面传递一个值。
-
我忘记写网址了。 jsfiddle.net/s35wmoLw/3
标签: jquery ajax jquery-select2 jquery-select2-4