问题很简单。
我有一个用 choice.js 制作的表单
为了创建多重选择,我使用此代码(来自 Choices Github)
var multipleCancelButton2 = new Choices('#choices-sites', {
removeItemButton: true,
choices: [
{value: 'Child ', label: 'Child ', selected: false},
{value: 'Child site', label: 'Child ', disabled: true},
{value: 'Child Three', label: 'Child Three'},
],
编辑有关信息(这是我的代码):
$.each(hotels, function(i, item) {
var myObject = { value: item.name, label: item.name, selected: 'false' };
choicesJsonh.push(myObject);
});
var textRemove = new Choices('#choices-hotels', {
editItems: true,
removeItemButton: true,
choices: choicesJsonh,
});
最佳答案
选择数组的内容不是字符串。不要尝试将其视为字符串!
如果你想创建一个新对象,那么:
var myObject = { value: item.name, label item.name, selected: false };
如果你想将其添加到数组中,那么:
myArray.push(myObject);
您可以组合这两个语句:
myArray.push({ value: item.name, label item.name, selected: false });
然后将数组本身放入传递给 Choices 函数的对象中:
var multipleCancelButton = new Choices('#choices-hotels', {
removeItemButton: true,
choices: myArray,
});
关于javascript - 在 Choices 输入对象中插入变量 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272377/