简体   繁体   English

从 SweetAlert2 选择框中获取价值?

[英]Get value from SweetAlert2 select box?

I have the following code... which is used for a sweet alert text box我有以下代码......用于甜蜜警报文本框

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

For some reason, it just returns "true" in the 'You selected' part...出于某种原因,它只是在“您选择的”部分返回“true”...

I want to get the item's id.我想获取项目的 id。

Example from swal2 official docs works fine.来自swal2 官方文档的示例工作正常。 Check your listOfItemsForSelectBox , perhaps it has some wrong format.检查您的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>

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

should be应该是

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

if you output 'result' to a console you will see all the data that is associated with the 'result' variable.如果您将“result”输出到控制台,您将看到与“result”变量关联的所有数据。 (ie console.log(result)) (即console.log(结果))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM