gpt4 book ai didi

javascript - jQuery Select2 : [object Window] in tags value

转载 作者:行者123 更新时间:2023-11-28 10:17:28 25 4
gpt4 key购买 nike

我使用 Select2 插件来显示标签。

JS:

$(document).ready(function () {
$("#tags").select2({
tags: true,
minimumInputLength: 3,
tokenSeparators: [','],
createSearchChoice: function (term) {
return {
id: $.trim(term),
text: $.trim(term)
};
},
ajax: {
url: "./controller/citynames.php",
dataType: "json",
data: function (term, page) {
return {
q: term
};
},
results: function (data, page) {
return {
results: data
};
}
},

// Take default tags from the input value
initSelection: function (element, callback) {
var data = [];

function splitVal(string, separator) {
var val, i, l;
if (string === null || string.length < 1) return [];
val = string.split(separator);
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
return val;
}

$(splitVal(element.val(), ",")).each(function () {
data.push({
text: this
});
});

callback(data);
},

// Some nice improvements:

// max tags is 3
maximumSelectionSize: 5,

// override message for max tags
formatSelectionTooBig: function (limit) {
return "Max tags is only " + limit;
}
});
});

HTML:

<input type="hidden" id="tags" value="test1,test2," style="width: 400px;">

现在我有两个问题:

1- 我的 html 操作我有两个 value="test1,test2," 但在 html 输出中我只看到一个值:test1 !!

2- 我检查元素,我看到 3 个值:test1,test2,[object Window]

图片:(这个演示@我的本地主机)

enter image description here

演示 @ JsFiddle

我该如何解决这个问题?!

最佳答案

你需要在你的initSelection中添加id

data.push({
id:this,
text: this
});

演示:http://jsfiddle.net/X6V2s/13/

关于javascript - jQuery Select2 : [object Window] in tags value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com