作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个甜蜜的警报输入来要求用户输入一个值。然后我想将值保存为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)
}
这就是我想做的:
我不确定我的解释是否正确,但任何想法或建议将不胜感激。
最佳答案
基于文档 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/
我是一名优秀的程序员,十分优秀!