使用下拉菜单更改货币

时间:2019-07-31 06:26:03

标签: javascript jquery codeigniter

我想知道是否可以使用下拉选择按钮来更改货币?

  

示例:

     
      
  • 印尼盾(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),并且我想通过从下拉菜单中进行选择,将货币从Rp更改为Usd。如果我必须使用js,请告诉我,因为我对js确实很陌生

0 个答案:

没有答案
相关问题