【发布时间】:2021-07-29 17:54:25
【问题描述】:
function order(id){
const { value: numberinput } = await Swal.fire({
title: 'You are ordering' + id + 'are you sure?' ,
text: 'When you press the "Order!" button, your order will be processed.',
icon: 'warning',
showCancelButton: true,
cancelButtonColor: '#d33',
confirmButtonColor: '#3bc42b',
cancelButtonText: 'Cancel',
confirmButtonText: 'Order!',
reverseButtons: true,
input: 'number',
inputLabel: 'portion size: \n ',
inputPlaceholder: 'Enter portion',
inputAttributes: {
min: 1,
max: 20,
step: 1
},
inputValue: 1,
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
title: 'Your order has taken!',
text: 'Your order of ${numberinput} portions is being prepared',
icon: 'success',
confirmButtonText: 'OK!'
})
} else if (result.isDismissed)
Swal.fire({
title: 'Abandoned!',
text: 'Your order of ${numberinput} servings has been abandoned',
icon: 'error',
confirmButtonText: 'OK!'
})
})
}
这不起作用,并且有一个 swal2 示例:
const ipAPI = '//api.ipify.org?format=json'
const inputValue = fetch(ipAPI)
.then(response => response.json())
.then(data => data.ip)
const { value: ipAddress } = await Swal.fire({
title: 'Enter your IP address',
input: 'text',
inputLabel: 'Your IP address',
inputValue: inputValue,
showCancelButton: true,
inputValidator: (value) => {
if (!value) {
return 'You need to write something!'
}
}
})
if (ipAddress) {
Swal.fire(`Your IP address is ${ipAddress}`)
}
我想问题是我没有写 if (numberinput) 但我不知道如何将 if(number) 和 if(result.isConfirmed)/if(result.isDismissed) 一起写。有人可以帮我吗?我尝试了很多东西,但都失败了。
【问题讨论】:
-
fetch(ipAPI)是异步的,不确定您希望如何使用其中的 vale。没有等待,因此您不会从 fetch 中获取返回值 -
不要混用
await和.then() -
@epascarello 他不是,他从sweetalert2.github.io/#examples 复制了它作为如何使用 sweetalert2 获取输入的示例。