js变量的甜蜜警报输入

sweet alert input to js variable

我想要一个甜蜜的警报输入来要求用户输入一个值。然后我想将该值保存为一个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)


}

这是我想做的:

  1. 要求用户输入。
  2. 获取输入并转换为 JS 变量。

我不确定我是否解释正确,但如果有任何想法或建议,我们将不胜感激。

根据文档 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)
        })
    }
}