【发布时间】:2021-03-23 00:07:58
【问题描述】:
我有两个输入标签和两个选择下拉标签,所以我希望输入值和下拉列表中的选定值始终在同一页面上的结果范围上更新(onchange 和 keyup)。这是一个货币转换器项目。
$(document).ready(function () {
var baseCurrency = "BTC";
var pay_amount = 1;
var targetCurrency = "ETH";
var get_amount;
$("#crypto12").html(targetCurrency);
$("#crypto14").html(targetCurrency);
$("#crypto16").html(targetCurrency);
$("#crypto13").html(baseCurrency);
$("#crypto15").html(baseCurrency);
$("#crypto17").html(baseCurrency);
$("#crypto19").html(targetCurrency);
var url;
currencyConverter(baseCurrency, pay_amount,targetCurrency,get_amount)
// some variables
$("#crypto1").ddslick({
imagePosition: "left",
selectText: "BTC",
onSelected: function (data) {
$("#selected").html((baseCurrency = data.selectedData.value));
currencyConverter(baseCurrency,pay_amount,targetCurrency,get_amount );
},
});
$("#crypto2").ddslick({
imagePosition: "left",
selectText: "ETH",
onSelected: function (data) {
$("#selected").html((targetCurrency = data.selectedData.value));
currencyConverter2(baseCurrency,pay_amount,targetCurrency,get_amount );
},
});
// currencyConverter(baseCurrency, pay_amount, targetCurrency, get_amount);
// get base currency value
$("#crypto1").change(function () {
// base currency
baseCurrency = $(this).children("option:selected").val();
// call currencyConverter function
currencyConverter(baseCurrency,pay_amount,targetCurrency,get_amount );
});
// get base currency number
$("#pay_amount").keyup(function () {
// base number
pay_amount = $(this).val();
// call currencyConverter function
currencyConverter(baseCurrency,pay_amount,targetCurrency,get_amount );
});
// get target currency value
$("#crypto2").change(function () {
// target currency
targetCurrency = $(this).children("option:selected").val();
// call currencyConverter function
currencyConverter2(baseCurrency,pay_amount,targetCurrency,get_amount );
});
// get target currency number
$("#get_amount").keyup(function () {
// target number
get_amount = $(this).val();
// call currencyConverter function
currencyConverter2(baseCurrency,pay_amount,targetCurrency,get_amount );
});
function currencyConverter(baseCurrency,pay_amount,targetCurrency,get_amount) {
// api url
var url = "https://coinlib.io/api/v1/coin?key=659b81f02b22b218&pref=" + targetCurrency + "&symbol=" + baseCurrency;
// make a get request to API
$.get(url, function (data) {
console.log(data.price)
//for (let [key, value] of Object.entries(data.price)) {
//console.log(data.price);
var result = parseFloat(data.price * pay_amount).toFixed(4);
var divobj = document.getElementById("get_amount");
divobj.value = result;
$("#get_amount").html($("#get_amount").val());
$("#crypto12").html(targetCurrency);
$("#crypto14").html(targetCurrency);
$("#crypto16").html(targetCurrency);
$("#crypto19").html(targetCurrency);
$("#crypto16")
.parent()
.find("#result")
.html($("#get_amount").val());
$("#crypto14")
.parent()
.find("#result")
.html($("#get_amount").val());
// }
//console.log(`data.price.${targetCurrency}`)
});
}
//currencyConverter(baseCurrency, pay_amount, targetCurrency, get_amount);
function currencyConverter2(baseCurrency, pay_amount, targetCurrency, get_amount) {
// api url
var url = "https://coinlib.io/api/v1/coin?key=659b81f02b22b218&pref=" + baseCurrency + "&symbol=" + targetCurrency;
// make a get request to API
$.get(url, function (data) {
console.log(data.price)
for (let [key, value] of Object.entries(data.price)) {
// console.log(value)
var result = parseFloat(data.price * get_amount).toFixed(4);
var divobj = document.getElementById("pay_amount");
divobj.value = result;
$("#pay_amount").html($("#pay_amount").val());
$("#crypto13").html(baseCurrency);
$("#crypto15").html(baseCurrency);
$("#crypto17").html(baseCurrency);
$("#crypto17")
.parent()
.find("#result")
.html($("#pay_amount").val());
$("#crypto15")
.parent()
.find("#result")
.html($("#pay_amount").val());
}
//console.log(`data.price.${targetCurrency}`)
});
}
//currencyConverter(baseCurrency, pay_amount, targetCurrency, get_amount);
});
这里是完整的代码at JSfiddle。
【问题讨论】:
-
似乎有效?
-
不工作目标货币工作但基础货币不工作,与支付金额相同,获取金额,他们应该在keyup或onchange上更新
-
我能够让它工作。我不能说在转换方面逻辑是正确的。我得到 1 BTC 价值 32 LTC。
标签: javascript jquery node.js