【发布时间】:2014-06-06 19:13:00
【问题描述】:
我使用 select2 作为标签输入,但在处理新标签的创建和将新标签 id 重新放入 select2 时,我感到很困惑
这个问题密切相关Select2.js: why is id the same as text on change for removed?
你可以从他的 jsfiddle http://jsfiddle.net/7e8Pa/ 中看到,当在 createSearchChoice 中找不到文本时,会创建新选项,id:-1,text:term,然后在 on change 事件中,将该 id 更改为 5
我需要能够向服务器提交$.post,并取回一个 id 而不是使用静态 5
问题是,如果我在 createsearchoption 中提交帖子,则在标签表中找不到的每个击键都会创建一个新标签,并尝试在 on change 事件中发布帖子,我假设 change 事件在 ajax 返回之前完成
.on("change", function(e) {
log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));
if (e.added) { // if its got an add obj
if (isNumeric(e.added.id)){
//if its not an existing tag , it passes a string instead of an id
// so just do a regular add
add(e.added.id);
} else {
//get the term text, post to server, return the new tag id
$.post('handlers/tags.ashx',
{ operation:"select2createtag",
text: $.trim(e.added.id.substring(3, e.added.id.length))} ,
function(data){
add(data.id);
});
};
【问题讨论】:
-
我觉得伙计,我已经浏览了文档和至少十几个表格。同样的问题:stackoverflow.com/questions/25150065/…。找到解决方案了吗?
-
我确实找到了解决方案,我会在下面发布我的答案并看看你的问题
标签: javascript jquery ajax jquery-select2