[英]how can i remove onkeyup on my javascript code
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.当我尝试在 usd 输入上添加
value="444"
的特定值时,它不会转换为其他值eth ,它仅在我手动输入金额时才起作用。 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>
Try this尝试这个
<script type="text/javascript">
$(document).ready(function () {
function getJSONData() {
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;
});
}
getJSONData();
$(".currencyField").keyup(function () {
//input[name='calc']
getJSONData();
});
});
</script>
First of all, you must change the function out of keyup
, because we don't do it.首先,您必须将函数从
keyup
中更改出来,因为我们不这样做。 The function works when you enter value.该功能在您输入值时起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.