0

现在它可以工作了......但是我怎样才能为 json 编写正确的调用?

我尝试使用此直接链接但不起作用,也许我必须编辑“数据”参数?我不知道,因为我读到的是可选参数,对吗?有什么帮助吗?

并且我可以设置我想要选择的 json 值的连续引用,以使它们始终更新(每 60 秒)。

谢谢!!

function itemManager(){
    var _me        = null;
    var _goldAsk   = 0;
    var _silverAsk = 0;
    var _eurusdAsk = 0;

    var construct = function(){
        //init here
        _me = $(this);
        _bindHandlers();
        _updateItem(125);
    }

    var _bindHandlers = function(){

        _me.on('stockupdated', function(){
            $('#gcost').text(_goldAsk + ' ' + _silverAsk + ' ' + _eurusdAsk);
        });

        $('#calculate').on('click', function(){
            alert("item gold ask(" + _goldAsk + ") vs silver ask(" + _silverAsk + ")");
        });

        $('#updateitem').on('click', function(){
            _updateItem(250);
        });

        $('#btnGetTotals').on('click', function(){
            var grammsOfGold   = $('#goldGramms').val().length   ? parseInt($('#goldGramms').val())   : 0;
            var grammsOfSilver = $('#silverGramms').val().length ? parseInt($('#silverGramms').val()) : 0;
            var carati = document.getElementById("carati").selectedIndex;

            alert('Total for gold: ' + (((( _goldAsk / 31.1034768 ) / _eurusdAsk) * (document.getElementsByTagName("option")[carati].value)) * grammsOfGold ).toFixed(2) + ' total for silver: ' + (grammsOfSilver * _silverAsk) + 'Carati selezionati: ' + (document.getElementsByTagName("option")[carati].value))
        });        

        $('#btnGetInfo').on('click', function(){
            alert('Gold ask: ' + ((_goldAsk / 31.1034768) / _eurusdAsk).toFixed(3) + ' silver ask: ' + ((_silverAsk / 31.1034768) / _eurusdAsk).toFixed(3)); 
        });
    }

    var _getMonth = function(){
        return parseInt($('#month').val());
    }

    var _updateItem = function(cost){
        $.ajax({
            type: 'POST',
            url: 'https://quotes.instaforex.com/get_quotes.php?m=json&q=gold,silver,eurusd',

            data: {
                json: '{' +
                    '"GOLD":{"symbol":"GOLD","lasttime":1379116798,"digits":2,"change":"-0.09","bid":"1325.91","ask":"1326.51"},' +
                    '"SILVER":{"symbol":"SILVER","lasttime":1379116779,"digits":3,"change":"-0.010","bid":"22.170","ask":"22.210"},' +
                    '"EURUSD":{"symbol":"EURUSD","lasttime":1379116796,"digits":4,"change":"-0.0001","bid":"1.3296","ask":"1.3299"}' +
                '}',
                delay: 2
            },

            success: function(data){                
                _goldAsk   = parseFloat(data.GOLD.ask);
                _silverAsk = parseFloat(data.SILVER.ask);
                _eurusdAsk = parseFloat(data.EURUSD.ask);
                _me.trigger('stockupdated');
            }
        })
    }

    construct();
}

window.onload = function(){
    var item = new itemManager();
}
4

3 回答 3

0

一切都与范围有关。在外部声明变量(例如,在页面顶部)。然后在没有var声明的情况下分配值:

// Make them global
var goldAsk, silverAsk, eurusdAsk;

$(document).ready(function(){

// segnalato dove andare a cercare ora definisco gli oggetti    
$.getJSON('json.php', function(json) {
 var gold = json.GOLD;
 var silver = json.SILVER;
 var eurusd = json.EURUSD;

  goldAsk = ((gold.ask / 31.1034768) / eurusdAsk).toFixed(3);
  silverAsk = ((silver.ask / 31.1034768) / eurusdAsk).toFixed(3);

  // operazioni di conversione
  eurusdAsk = eurusd.ask;

 // pubblico in DIV tramite funzione .text
  $(".container-quote").text('ORO: ' + goldAsk  + '   ' + 'ARGENTO: ' + silverAsk + '     *Quotazioni €/g in tempo reale aggiornamento ogni 3s. Quotazioni da www.instaforex.com    ' + '      ' + '        Quotazioni del cambio $/€ aggiornate in tempo reale dallo stesso database');

$('.grammibox input').keyup(multiply);
$('span.caratiselect select').change(multiply);
 });

function multiply() {
    var grammi = parseFloat($('.grammibox input').val());

    var carati = $('span.caratiselect select').val().substr(1);

    $('#totale').text('La tua valutazione: ' + ((goldAsk * carati) * grammi).toFixed(2));
}
});
于 2013-09-14T00:19:23.097 回答
0

将变量设置在其他函数可以引用的范围内。

window.goldAsk = true;
window.silverAsk = { some: 'value' };

这将使它们全球化。如果您要这样做,我会创建一个对象来保留与此特定脚本有关的所有内容,例如:

window.myProgram = {};

然后你可以简单地:

myProgram.goldAsk = true;
于 2013-09-14T00:13:58.733 回答
0

试试这个:http: //jsfiddle.net/vp9V6/

HTML

<div>Gold cost: <span id="gcost"></span></div>
months: <input type="text" id="month" />
<input type="button" id="calculate" value="GET TAXES" />
<input type="button" id="updateitem" value="UPDATE ITEM" />

Javascript

function itemManager(){
    var _me     = null;
    var _item   = {};

    var construct = function(){
        //init here
        _me = $(this);
        _bindHandlers();
        _updateItem(125);
    }

    var _bindHandlers = function(){

        _me.on('stockupdated', function(){
            $('#gcost').text(_item.cost);
        });

        $('#calculate').on('click', function(){
            alert("item cost(" + _item.cost + ") * month(" + _getMonth() + ") = " + (_item.cost * _getMonth()));
        });

        $('#updateitem').on('click', function(){
            _updateItem(250);
        });
    }

    var _getMonth = function(){
        return parseInt($('#month').val());
    }

    var _updateItem = function(cost){
        $.ajax({
            type: 'POST',
            url: '/echo/json/',
            data: {
                json: '{"cost":"' + cost + '","currency":"USD"}',
                delay: 2
            },
            success: function(data){                
                _item.cost     = parseInt(data.cost);
                _item.currency = data.currency;
                _me.trigger('stockupdated');
            }
        })
    }

    construct();
}

window.onload = function(){
    var item = new itemManager();
}
于 2013-09-14T14:58:21.410 回答