我想要一个甜蜜的警报输入来要求用户输入值。然后我想将值保存为JS变量,稍后再使用。
let coinName = swal("Enter cryptocurrency name", {content: "input"})
console.log(coinName)
这是我的完整代码:
function getPrices() {
let coinName = swal("Enter cryptocurrency name", {content: "input"})
console.log(coinName.value)
$.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('https://api.coinmarketcap.com/v1/ticker/')+ coinName.valueOf + '/' + '&callback=?', function (data) {
console.log(data.contents);
})
var coinPrice = data.contents[0].price_usd
swal('Currently, in USD, 1 ' + coinName + ' is equal to $' + coinPrice)
}
这就是我想要做的事情:
我不确定我是否正确解释了这一点,但我们将非常感谢任何想法或建议。
答案 0 :(得分:4)
根据文档https://sweetalert.js.org/guides/#using-dom-nodes-as-content,您可以使用
执行此操作swal("Write something here:", {
content: "input",
})
.then((value) => {
// do something with value variable
});
如果我们更新您的代码
function getPrices() {
swal("Enter cryptocurrency name", {content: "input"}).then(function(coinName) {
$.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('https://api.coinmarketcap.com/v1/ticker/')+ coinName + '/' + '&callback=?', function (data) {
console.log(data.contents);
var coinPrice = data.contents[0].price_usd
swal('Currently, in USD, 1 ' + coinName + ' is equal to $' + coinPrice)
})
}
}