首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fx.convert不与Money.js一起使用

fx.convert不与Money.js一起使用
EN

Stack Overflow用户
提问于 2013-12-18 01:27:07
回答 2查看 1K关注 0票数 0

使用chrome的开发工具,我可以看到我的index.xhtml显示状态200OK,但当我点击我的转换按钮时,转换后的金额没有显示出来。下面是我的xhtml文件:

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Currency Exchange Rate Calculator</title>
        <meta charset="utf-8"/>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="money.js"></script>
        <script src="accounting.js"></script>
        <script src="http://openexchangerates.org/api/latest.json?app_id=e6b775e4bbde4ae19dd7294c39d71b15"></script>
        <script src="http://openexchangerates.org/api/currencies.json?app_id=e6b775e4bbde4ae19dd7294c39d71b15"></script>
        <script src="http://openexchangerates.org/api/historical/2011-10-18.json?app_id=e6b775e4bbde4ae19dd7294c39d71b15"></script>
        <link rel="StyleSheet"  type="text/css"  href="apricot.css"></link>
        <script type="text/javascript">
            // Load exchange rates data via the cross-domain/AJAX proxy:
            $(document).ready(function () {
            function load(){
                $.getJSON(
                'http://openexchangerates.org/latest.json?app_id=e6b775e4bbde4ae19dd7294c39d71b15',
                function(data) {
                    // Check money.js has finished loading:
                    if ( typeof fx != "undefined") {
                        fx.rates = data.rates;
                        fx.base = data.base;
                        alert("fx loaded");
                    } else {
                        // If not, apply to fxSetup global:
                        alert("fx not loaded");
                        var fxSetup = {
                            rates : data.rates,
                            base : data.base
                        }
                    }
                }
            ).error(function(error){console.log(error);});
                return false;    
            }
});
            function convert(){
                //load();
                var fromelement = document.getElementById('xrate:fromcur');
                var from=fromelement.value;
                var toelement = document.getElementById('xrate:tocur');
                var to = toelement.value;
                var amtelement =document.getElementById('xrate:amt');
                var amt = amtelement.value;
                alert("from = "+from+" to = "+to+" amount = "+amt);

                //fx.settings = { from: from, to: to };
                var conv=fx.convert(amt,{ from: from, to: to });
                alert("conv = "+conv);
                //fx.settings = { from: from, to: to };
                //var conv=fx.convert(amt); // 647.71034
                var convelement = document.getElementById('xrate:result');
                convelement.value=conv;

            }

        </script>
    </h:head>
    <h:body>
        <div id="welcome">
            <h:form id="xrate">
                <h:panelGrid columns="2" style="background-color: yellow">

                    <h:outputLabel value="Select Currency From" for="fromcur" />
                    <h:selectOneListbox  id="fromcur" size = "1" value="xxx" title="xxx" >
                        <f:selectItem id="si0" itemLabel=" " itemValue=" " />
                        <f:selectItem id="si1" itemLabel="RON" itemValue="RON" />
                        <f:selectItem id="si2" itemLabel="EUR" itemValue="EUR" />
                        <f:selectItem id="si3" itemLabel="CAD" itemValue="CAD" />
                        <f:selectItem id="si4" itemLabel="GBP" itemValue="GBP" />
                        <f:selectItem id="si5" itemLabel="USD" itemValue="USD" />
                        <f:selectItem id="si6" itemLabel="MXP" itemValue="MXP" />
                        <f:selectItem id="si7" itemLabel="CNY" itemValue="CNY" />
                    </h:selectOneListbox>
                    <h:outputLabel value="Select Currency To" for="tocur" />
                    <h:selectOneListbox  id="tocur" size = "1" value="xxx" title="xxx" >
                        <f:selectItem id="ti0" itemLabel=" " itemValue=" " />
                        <f:selectItem id="ti1" itemLabel="RON" itemValue="RON" />
                        <f:selectItem id="ti2" itemLabel="EUR" itemValue="EUR" />
                        <f:selectItem id="ti3" itemLabel="CAD" itemValue="CAD" />
                        <f:selectItem id="ti4" itemLabel="GBP" itemValue="GBP" />
                        <f:selectItem id="ti5" itemLabel="USD" itemValue="USD" />
                        <f:selectItem id="ti6" itemLabel="MXP" itemValue="MXP" />
                        <f:selectItem id="ti7" itemLabel="CNY" itemValue="CNY" />

                    </h:selectOneListbox>

                    <h:outputLabel value="Enter Amount" for="amt" />
                    <h:inputText id="amt"   style="color:red;background-                      color:yellowgreen" size="10"/>
                    <h:commandButton value="Calculate" id="calc" onclick="convert()"/>
                    <h:outputText id ="result"  style="color:blue;border:5px solid tan;" />

                </h:panelGrid>
            </h:form>
        </div>
    </h:body>
</html>

enter code here

我正在使用openexchangerates.org的链接,任何帮助都将不胜感激。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-12-19 23:54:04

我在代码中发现了错误。因此,对于任何感兴趣的人来说,现在可以很好地使用它:

代码语言:javascript
复制
            <script type="text/javascript">
        // Load exchange rates data via the cross-domain/AJAX proxy:
        //           $(document).ready(function () {
        $.getJSON(
        'http://openexchangerates.org/latest.json?app_id=e6b775e4bbde4ae19dd7294c39d71b15',
        function(data) {
            // Check money.js has finished loading:
            if ( typeof fx != "undefined") {
                fx.rates = data.rates;
                fx.base = data.base;
                //alert("fx loaded");
            } else {
                // If not, apply to fxSetup global:
                //alert("fx not loaded");
                var fxSetup = {
                    rates : data.rates,
                    base : data.base
                }
            }
        }
    )


        //           });
        function convert(){
            //load();
            var fromelement = document.getElementById('xrate:fromcur');
            var from=fromelement.value;
            var toelement = document.getElementById('xrate:tocur');
            var to = toelement.value;
            var amtelement =document.getElementById('xrate:amt');
            var amt = amtelement.value;
            alert("from = "+from+" to = "+to+" amount = "+amt);

            //fx.settings = { from: from, to: to };
            var conv=fx.convert(amt,{ from: from, to: to });
            alert("conv = "+conv);
            //fx.settings = { from: from, to: to };
            //var conv=fx.convert(amt); // 647.71034
            var convelement = document.getElementById('xrate:result');
            convelement.value=conv;
            //document.getElementById('xrate:result').value=conv;

        }
票数 0
EN

Stack Overflow用户

发布于 2020-04-08 03:36:33

从money //React组件导入外汇

代码语言:javascript
复制
handleSubmit(){
        /*require(["money"], function(fx) {
            console.log(fx.convert(1000, {from: "GBP", to: "HKD"}))
        })*/
        fx.base = "USD";
        fx.rates = {
            "EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
            "GBP" : 0.647710, // etc...
            "USD" : 1,        // always include the base rate (1:1)
            /* etc */
        }
        console.log(fx(1.99).from("USD").to("GBP"))
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20640789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档