我想为SweetAlert2输入类型选择添加动态选项,如下所示:
swal({
title: 'Select Ukraine',
input: 'select',
inputOptions: {
'SRB': 'Serbia',
'UKR': 'Ukraine',
'HRV': 'Croatia'
},
inputPlaceholder: 'Select country',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve, reject) {
if (value === 'UKR') {
resolve();
} else {
reject('You need to select Ukraine :)');
}
});
}
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
});
})
我想添加自己保存在对象中的选项,而不是这三个国家。我怎么能用javascript做到这一点?
答案 0 :(得分:1)
您可以尝试像这样构建选项对象。
function linkThing() {
//this i assume would be loaded via ajax or something?
var myArrayOfThings = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];
var options = {};
$.map(myArrayOfThings,
function(o) {
options[o.id] = o.name;
});
swal({
title: 'My Title',
text: 'Please select an option',
input: 'select',
inputOptions: options,
showCancelButton: true,
animation: 'slide-from-top',
inputPlaceholder: 'Please select'
}).then(function (inputValue) {
if (inputValue) {
console.log(inputValue);
}
});
};