如何在JavaScript代码上删除OnKeyup
当我尝试在USD输入上添加value =“ 444”
的特定值,它不会转换为其他值 eth ,它仅在我手动输入金额时才能工作。在代码中已经指定的值时,该如何转换
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="eth" class="currencyField" placeholder="ETH">
<div class="arrow" style="margin: 0 10px";>=</div>
<input type="number" name="usd" value="444" class="currencyField" value="" placeholder="USD">
</div><span id="price"></span>
<script type="text/javascript">
$(".currencyField").keyup(function(){ //input[name='calc']
let convFrom;
if($(this).prop("name") == "eth") {
convFrom = "eth";
convTo = "usd";
}
else {
convFrom = "usd";
convTo = "eth";
}
$.getJSON( "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum",
function( data) {
var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
var exchangeRate = parseInt(data[0].current_price);
let amount;
if(convFrom == "eth")
amount = parseFloat(origAmount * exchangeRate);
else
amount = parseFloat(origAmount/ exchangeRate);
$("input[name='" + convTo + "']").val(amount.toFixed(2));
price.innerHTML = amount
});
});
</script>
</body>
</html>
when i try to add a specific value with value="444"
on the usd input it does not convert to the other value eth it's only working when i enter manually the amount. how can i convert when it's value already specified in the code
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="eth" class="currencyField" placeholder="ETH">
<div class="arrow" style="margin: 0 10px";>=</div>
<input type="number" name="usd" value="444" class="currencyField" value="" placeholder="USD">
</div><span id="price"></span>
<script type="text/javascript">
$(".currencyField").keyup(function(){ //input[name='calc']
let convFrom;
if($(this).prop("name") == "eth") {
convFrom = "eth";
convTo = "usd";
}
else {
convFrom = "usd";
convTo = "eth";
}
$.getJSON( "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum",
function( data) {
var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
var exchangeRate = parseInt(data[0].current_price);
let amount;
if(convFrom == "eth")
amount = parseFloat(origAmount * exchangeRate);
else
amount = parseFloat(origAmount/ exchangeRate);
$("input[name='" + convTo + "']").val(amount.toFixed(2));
price.innerHTML = amount
});
});
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先尝试
一下,您必须将功能更改为
keyup
,因为我们不这样做。输入值时,该功能起作用。Try this
First of all, you must change the function out of
keyup
, because we don't do it. The function works when you enter value.