2016-12-20 98 views
-3

我在網站上有一個貨幣計算器我正在努力。我讓它工作得很好,以便根據您使用的輸入正確輸出兩種貨幣。輸出一個javascript變量

但是現在我需要一條線來顯示計算髮生後顯示的確切匯率。

因此,不是顯示用戶輸入的內容,而是顯示1 GBP在目標貨幣中的價值。第一個結果將總是1英鎊,因爲這是唯一使用的基礎貨幣。

因此,例如: 1 GBP = X在目標貨幣

(例如: 「1 GBP是等於1.24 USD」 或1 GBP是等於1.5 EUR)

原始HTML低於

<script type="text/javascript"> 
(function() { 
var js = document.createElement('script'); js.type = 'text/javascript'; js.async = true; 
js.src = 'http://www.floatrates.com/scripts/converter.js'; 
var sjs = document.getElementsByTagName('script')[0];  sjs.parentNode.insertBefore(js, sjs); 
})(); 
</script> 

<script> 
$(document).ready(function() { 
$("#from").msDropdown(); 
}) 
</script> 

<script> 
$(document).ready(function() { 
$("#to").msDropdown(); 
}) 
</script> 

<form class="frConverter"> 
    <div class="field-row"> 
    <div class="from"> 
     <label for="from">Sending From:</label> 
     <select id="from" value="gbp" name="base_currency" class="select-box"> 
     <option value="GBP" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/uk.gif" data-imagecss="flag" selected="">United Kingdom Pounds - GBP</option> 
     </select> 
     <input type="text" name="base_value" size="10" value="1"> 
     <input type="hidden" name="default_base" value="gbp"> 
    </div> 

    <div class="to"> 
     <label for="to">Sending To:</label> 
     <select id="to" value="USD" name="target_currency" class="select-box"> 
     <option value="USD" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/us.gif" data-imagecss="flag">United States (Dollar) - USD</option> 
     <option value="GBP" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/uk.gif" data-imagecss="flag">United Kingdom (Pound) - GBP</option> 
    </select>    
    <input type="hidden" name="default_target" value="usd"> 
    <input type="text" name="target_value" size="10" value=""> 
    </div> 
    </div> 
</form> 
+2

可以包含足夠的代碼使計算器的工作,並將其納入一個單一的片段中?使用帶有圖標的工具欄按鈕和'< >'就可以了 – mike510a

+0

當然,我現在編輯了代碼 – pasinionline

+0

代碼..在哪裏顯示結果? –

回答

1

我不能得到這個工作,關上輸入的更改事件或事件的內容事件(即時猜測的floatrates腳本以某種方式劫持這些事件),所以這是目前射擊任何改變document

希望這會有所幫助。

$(document).change(function() { 
 
    getSingleExchange(); 
 
}); 
 

 

 
function getSingleExchange() { 
 
    var fromCurr = $("#from option:selected").val(); 
 
    var toCurr = $("#to option:selected").val(); 
 
    var from = $('input[name=base_value]').val(); 
 
    var to = $('input[name=target_value]').val(); 
 

 
    rate = parseInt(from)/parseInt(to); 
 

 
    output = fromCurr + " to " + toCurr + "= " + rate.toFixed(2); 
 

 
    $("#singleExchangeValue").html(output); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script type="text/javascript" async="" src="http://www.floatrates.com/scripts/converter.js"></script> 
 

 
<form class="frConverter"> 
 
    <div class="field-row"> 
 
    <div class="from"> 
 
     <label for="from">Sending From:</label> 
 
     <select id="from" value="gbp" name="base_currency" class="select-box"> 
 
     <option value="GBP" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/uk.gif" data-imagecss="flag" selected="">United Kingdom Pounds - GBP</option> 
 
     </select> 
 
     <input type="text" id="base_value" name="base_value" size="10" value="1"> 
 
     <input type="hidden" name="default_base" value="gbp"> 
 
    </div> 
 

 
    <div class="to"> 
 
     <label for="to">Sending To:</label> 
 
     <select id="to" value="USD" name="target_currency" class="select-box"> 
 
     <option value="USD" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/us.gif" data-imagecss="flag">United States (Dollar) - USD</option> 
 
     <option value="GBP" data-image="http://inaramoneytran.wpengine.com/wp-content/plugins/monex/assets/img/uk.gif" data-imagecss="flag">United Kingdom (Pound) - GBP</option> 
 
     </select> 
 
     <input type="hidden" name="default_target" value="usd"> 
 
     <input type="text" name="target_value" size="10" value=""> 
 
    </div> 
 
    </div> 
 
</form> 
 

 
<div id="singleExchangeValue"></div>

+0

這太棒了。感謝您的幫助,我當然可以用它作爲編輯它的基礎,以便我喜歡它。我非常感激。 – pasinionline

+0

您的歡迎。請記住標記爲接受的答案,如果這有幫助,並upvote:P – Brad