简体   繁体   English

甜蜜警报输入到js变量

[英]sweet alert input to js variable

I want to have a sweet alert input to ask the user to enter a value. 我想要一个甜美的警报输入,要求用户输入一个值。 Then I want to save the value as a JS variable, to be used later. 然后,我想将该值另存为JS变量,以供以后使用。

let coinName = swal("Enter cryptocurrency name", {content: "input"})
console.log(coinName)

Here is my full code: 这是我的完整代码:

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)


}

This what I want to do: 这是我想做的:

  1. Ask user for input. 要求用户输入。
  2. Take input and convert in into JS variable. 输入并转换为JS变量。

I'm not sure if I've explained this correctly, but any ideas or suggestions would be highly appreciated. 我不确定我是否正确解释了这一点,但是任何想法或建议都将不胜感激。

Based on the docs https://sweetalert.js.org/guides/#using-dom-nodes-as-content you can do this with 根据文档https://sweetalert.js.org/guides/#using-dom-nodes-as-content,您可以使用

swal("Write something here:", {
  content: "input",
})
.then((value) => {
  // do something with value variable
});

If we update your code 如果我们更新您的代码

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)
        })
    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM