gpt4 book ai didi

javascript - 甜蜜警报输入到 js 变量

转载 作者:行者123 更新时间:2023-11-28 14:36:45 24 4
gpt4 key购买 nike

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

关于javascript - 甜蜜警报输入到 js 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49662273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com