dounieliang4712 2017-06-17 11:02
浏览 46
已采纳

jQuery货币转换复选框点击使用谷歌财务api

I want my amount field to get updated with the converted value of the total amount (with amount I mean the sum of amount received from all checkboxes checked). For example, I have the following checkboxes.

<input type="checkbox" value="10">
<input type="checkbox" value="20">
<input type="checkbox" value="30">

These 10, 20 and 30 (total 60) values are in USD. I want it to be converted to INR and be displayed in a div like say <div class="convertedAmount">Rs.0.00</div>. For conversion I am using Google Finance API here as shown below.

<?php
$from_Currency = "USD";
$to_Currency = "INR";
$encode_amount = 1;
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
echo $converted_currency;
?>

Now the problem is that I want the div value to be updated each time when checkboxes are checked or unchecked using jquery. But I have weak hands on jQuery so I am having trouble. I would be grateful if helped. Please help me.

  • 写回答

2条回答 默认 最新

  • doushuangai9733 2017-06-17 13:59
    关注

    I figured out the solution after sometime I posted the question. Here, I simply stored the converted value in a variable inr and multiplied it with the total summed value received from each checkbox and displayed the total in INR. Now each time a checkbox is checked or unchecked it updates the values respectively.

    Enjoy the solution code below. Its fast too :D

    $(document).ready(function(){
      $('input[type=checkbox]').change(function(){
        var total = 0; var inr = '<?php echo $converted_currency; ?>';
        $('input:checkbox:checked').each(function(){
          total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
        });
        $("#costdisplay").html(total);
        $("#inrdisplay").html(total*inr);
        $("input[name=ap_amount]").val(total);
        $("input[name=payment]").val(total);
        $("input[name=amount]").val(total*inr);
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c