从 SweetAlert2 选择框中获取价值?

IWi*_*sta 4 javascript sweetalert sweetalert2

我有以下代码......用于甜蜜警报文本框

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
  })
})
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它只是在“您选择的”部分返回“true”...

我想获取项目的 id。

Ale*_*rff 6

来自swal2 官方文档的示例工作正常。检查你的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
  })
})
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)