如何在JavaScript代码上删除OnKeyup

发布于 2025-02-10 11:23:45 字数 1673 浏览 1 评论 0原文

当我尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-02-17 11:23:45

首先尝试

   <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>

一下,您必须将功能更改为keyup,因为我们不这样做。输入值时,该功能起作用。

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. The function works when you enter value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文