【问题标题】:Get value from SweetAlert2 select box?从 SweetAlert2 选择框中获取价值?
【发布时间】:2017-06-30 20:52:13
【问题描述】:

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

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。

【问题讨论】:

    标签: javascript sweetalert sweetalert2


    【解决方案1】:

    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>

    【讨论】:

    【解决方案2】:
    }).then(function (result) {
      swal({
        type: 'success',
        html: 'You selected: ' + result
      })
    })
    

    应该是

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-25
      • 2018-05-30
      • 2015-04-07
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2018-07-24
      相关资源
      最近更新 更多