Javascript code: dynamically change currencies with dropdown HTML

后端 未结 3 1026
小鲜肉
小鲜肉 2021-01-25 22:34

I have been looking for this all day - Change currencies throughout the page when choosing between countries or currencies from dropdown menu.

What I basically need is a

3条回答
  •  耶瑟儿~
    2021-01-25 23:02

    I would use jQuery, so feel free to disregard my answer if you don't want to use an external library. It can be found on www.jquery.com.

    First, you make a span for all places where currency should be changed, give it class "currency" and in the name attribute, you put the value in your "base currency". Example:

     499 
    

    Then you can make a button, say it has id "showInEuro".

    
    

    Then write some jQuery code similar to this:

    var usdToEuroExchRate = 1.5; // Obviously just a magic constant
    
    // When the button is clicked
    $("#showInEuro").click(function() {
         // Iterate through all of the currency spans
        $("span.currency").each(function(index) {
            // And take their names times the exchangerate and put it in the span.
            $(this).text($(this).attr("name") * usdToEuroExchRate);
        });
    });
    

    Of course, you should try to use real, live exchange rates.

    I made a JSFiddle for you: http://jsfiddle.net/An3v9/9/

提交回复
热议问题