我有以下代码...用于甜蜜警报文本框
swal({
title: 'Select an Item',
input: 'select',
inputOptions: listOfItemsForSelectBox,
inputPlaceholder: 'Select country',
showCancelButton: true,
inputValidator: function (value) {
return new Promise(function (resolve, reject) {
if (value != null) {
resolve()
}
})
}
}).then(function (result) {
swal({
type: 'success',
html: 'You selected: ' + result
})
})
出于某种原因,它只是在“您选择的”部分返回“true”...
我想获取项目的 ID。
最佳答案
示例来自 swal2 official docs工作正常。检查您的 listOfItemsForSelectBox
,可能格式有误。
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
})
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.5/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.5/sweetalert2.min.js"></script>
关于javascript - 从 SweetAlert2 选择框获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44854661/