4

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

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。

4

2 回答 2

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
  })
})
<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>

于 2017-06-30T21:10:05.433 回答
2
}).then(function (result) {
  swal({
    type: 'success',
    html: 'You selected: ' + result
  })
})

应该

}).then(function (result) {
  swal({
    type: 'success',
    html: 'You selected: ' + result.value
  })
})

如果您将“结果”输出到控制台,您将看到与“结果”变量关联的所有数据。(即console.log(结果))

于 2021-04-04T23:31:38.677 回答