【发布时间】:2014-06-11 06:56:05
【问题描述】:
我使用以下 sn-p 使用 ajax 远程添加新的select2 标签,我想在新标签/删除标签事件中注册或删除我的多对多表的一些记录
表格看起来像
---------------------------------
+--voucher_id--+|+--product_id--+
---------------------------------
+ 123 | 566 +
---------------------------------
+ 156 | 566 +
---------------------------------
+ 123 | 426 +
---------------------------------
+ 156 | 516 +
---------------------------------
我的 JavaScript
$(".e6").select2({
tags: true,
placeholder: 'placeholder',
minimumInputLength: 1,
ajax: {
url: 'searchProducts',
dataType: 'json',
data: function(term) {
return {q: term};
},
results: function(data) {
return {results: data};
}
},
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.computername.localeCompare(term) === 0;
}).length === 0) {
return {id: term, name: term};
}
},
formatResult: function(item, page) {
return item.computername;
},
formatSelection: function(item, page) {
return item.computername;
}
});
在返回的 json 中,我也有一个产品 ID,我正在寻找一种在 select2 事件上触发新 ajax 的方法,但我不知道应该在哪里保存或删除表中的数据。
通过一些研究,我已经能够构建一个函数来更新上表中的记录,并且到目前为止效果很好
$('.e6').on("change", function(e){
console.log(ids);
console.log(gs);
$.ajax({
type: "POST",
url: '/admin/?controller=vouchers&action=updateRelatedProducts',
data: {ids: ids, gs:gs},
error: function () {
alert("error");
}
});
});
但我无法使用初始现有标签填充我的输入字段
【问题讨论】:
-
将 ajax 函数抽象为它自己的函数,然后将其应用于您必须执行的任何 select2 函数。
-
我不清楚如何在 select2 的情况下捕获删除或添加事件
-
我刚看了
Select2,真是太棒了。将不得不开始使用它! -
@No1_Melman 其实和julesjanssen.github.io/chosen很像
标签: jquery ajax twitter-bootstrap jquery-select2