【发布时间】:2017-10-02 09:02:48
【问题描述】:
在 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
})
})
键值结构用于填充要选择的项目。有没有办法从本地文件中做到这一点?
我的特殊情况:我正在开发一个网站,该网站执行一些操作并使用来自数据库的一些信息。对我来说,创建一个具有此结构的 json 文件真的很容易:
[{
"tag1": "tag1",
"tag2": "tag2",
"tag3": "tag3"
}]
其中 tag[i] 可以是任何不跟随 tag[i] 结构的字符串。
在this question 中,我发现这可以使用 Promise 来执行:
var inputOptionsPromise = new Promise(function (resolve) {
setTimeout(function () {
resolve({
//place options here
})
}, 2000)
})
$(function(){
$("#taginfo").click(function(){
console.log("click on tag info");
swal({
title: 'Select Tag',
input: 'select',
inputOptions: inputOptionsPromise,
inputPlaceholder: 'Select tag',
showCancelButton: true,
inputValidator: function (value) {
return new Promise(function (resolve, reject) {
if (value != '') {
document.getElementById('taginfo').value = value;
resolve();
}else {
reject('You need to select one tag')
}
})
}
}).then(function (result) {
swal({
type: 'success',
html: 'You selected: ' + result
})
})
});
});
但是我不知道如何将json文件(位于/public/resources/tags.json下)加载到inputOptionsPromise解析函数中。
【问题讨论】:
标签: javascript json sweetalert sweetalert2