选择字段和自定义输入仅显示输入值sweetalert2

时间:2017-03-23 14:13:20

标签: javascript sweetalert

swal({
title: 'Please Select a contact to send money',
input: 'select',
inputOptions: {
'1': 'Jhon',
'2': 'Gemie',
'3': 'Paul'
},
inputPlaceholder: 'Select a contact',
html: 'Amount: <input id="amount" class="form-control swal2-input">',
preConfirm: function () {
return new Promise(function (resolve) {
    resolve(
        document.getElementById('amount').value,
    )
})
}
onOpen: function () {
$('#swal-input1').focus()
}
}).then(function (result) {
swal(JSON.stringify(result))
}).catch(swal.noop)

以上代码仅显示文本输入值,如何获得结果数组中的选择值。我使用SweetAlert2,不知道吗?谢谢!!

1 个答案:

答案 0 :(得分:2)

preConfirm函数的第一个参数应该是所选选项的值。

这意味着你可以做这样的事情:

&#13;
&#13;
swal({
  title: 'Please Select a contact to send money',
  input: 'select',
  inputOptions: {
  '1': 'Jhon',
  '2': 'Gemie',
  '3': 'Paul'
  },
  inputPlaceholder: 'Select a contact',
  html: 'Amount: <input id="amount" class="form-control swal2-input">',
  preConfirm: function (selectedOption) {
    return new Promise(function (resolve) {
        resolve({selectedOption: selectedOption, value: document.getElementById('amount').value})
    });
  },
  onOpen: function () {
    $('#swal-input1').focus();
  }
}).then(function (result) {
  console.log(result);
  swal(JSON.stringify(result))
}).catch(swal.noop)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/sweetalert2/6.4.4/sweetalert2.min.js"></script>
<link href="https://cdn.jsdelivr.net/sweetalert2/6.4.4/sweetalert2.css" rel="stylesheet"/>
&#13;
&#13;
&#13;

相关问题