现在它有效...但我怎么能为json写出正确的调用?
我试过这个直接链接但不起作用,也许我要编辑“数据”参数?我不知道,因为我读到的是可选参数吗?有什么帮助吗?
我可以设置我想要选择的json值的连续refres,以使它们始终更新(每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();
}
答案 0 :(得分:0)
将变量设置在其他函数可以引用的范围内。
window.goldAsk = true;
window.silverAsk = { some: 'value' };
这将使他们成为全球性的。如果你打算这样做,我会创建一个对象来保持与这个特定脚本的所有关系,如:
window.myProgram = {};
然后你可以简单地说:
myProgram.goldAsk = true;
答案 1 :(得分: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));
}
});
答案 2 :(得分: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();
}