首页 > 解决方案 > 使用下拉菜单更改货币

问题描述

所以我想知道有没有办法通过使用下拉选择按钮来改变货币?

例子 :

  • 印尼盾 (RP) 至 美元 (USD)

已经尝试使用 if-else 并且失败了我需要使用 jquery/javascript 吗?

我试试这个但还是不明白

    selector = document.getElementById("currencySelector");
var
    currencyElements = document.getElementsByClassName("currency");
var 
    usdChangeRate = {
      AUD: 1.0490, // 1AUD = 1.0490 USD
      EUR: 1.4407, // 1EUR = 1.4407 USD
      GBP: 1.6424,
      USD: 1.0
    };

selector.onchange = function () {
    var toCurrency = selector.value.toUpperCase();

    for (var i=0,l=currencyElements.length; i<l; ++i) {
        var el = currencyElements[i];
        var fromCurrency = el.getAttribute("data-currencyName").toUpperCase();

      if (fromCurrency in usdChangeRate) {
          var // currency change to usd
              fromCurrencyToUsdAmount = parseFloat(el.innerHTML) * usdChangeRate[fromCurrency];
          var  // change to currency unit selected
              toCurrenyAmount = fromCurrencyToUsdAmount / usdChangeRate[toCurrency];

          el.innerHTML = toCurrenyAmount + "<span>" + toCurrency.toUpperCase() + "</span>";
          el.setAttribute("data-currencyName",toCurrency);
      }
    }
};

我的下拉菜单:

<select name="matauang" class="form-control" onchange = "changecurrency()" id="matauang">
            <option value="null" selected disabled>Choose</option>
            <?php foreach($payment as $pay) : ?>
            <option value="<?php echo $pay["kurs"]; ?>"><?php echo $pay["kurs"]; ?></option>
            <?php endforeach; ?>
        </select>

我的桌子:


<table class="table table-fixed table-bordered table-hover" style="width:100%;" id="tebal">
                <thead>
                    <tr>
                        <th scope="col">Jumlah/Quantity </th>
                        <th scope="col">Unit Price </th>
                        <th scope="col">Sub Total </th>
                    </tr>
                </thead>
                <tbody>
                    foreach ($query as $kiki) : ?>
                        <tr class="table-row">
                            <td><?php echo $kiki["qty_op"]; ?></td>
                            <td><?php echo $kiki["price"]; ?></td>
                            <td class="calc"><?php $total = $kiki["qty_op"]*$kiki["price"]; echo $total; ?></td>
                        </tr>
                    <?php endforeach;?>
                </tbody>
            </table>

是否有任何方法可以将表Sub Total值更改为美元?因为我制作的格式是,我想通过从下拉列表中选择Rupiah (RP)来更改货币。如果我必须使用请告诉我,因为我真的很陌生RpUsdjsjs

标签: javascriptjquerycodeigniter

解决方案


推荐阅读