我一直對quarterStr
未定義?我試圖在getNexthreeMonths
函數中訪問它。所以一個月從下拉菜單中選擇,然後我要訪問的getNexthreeMonths
結果:JQuery事件以外的變量範圍
getNextThreeMonths: function() {
$("#date").on('change', function (e){
var currentMonth = $(this).find('option:selected').attr('id');
var currentMonth = parseInt(currentMonth);
var qNum = Math.floor(currentMonth/3);
var qArr = ["Jan","Apr","Jul","Oct"];
var quarterStr = qArr[ (qNum+1) % 4 ] + "," + qArr[ (qNum+2) % 4 ] + "," + qArr[ (qNum+3)%4 ];
});
console.log(quarterStr);
}
嘗試移動console.log(quarterStr)}};在你的代碼中。它看起來像在你定義的匿名函數之外。 –
雖然我想訪問getNextThreeMonths:函數中的quarterStr,但不知道我是否理解 –
javascript範圍:http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Lee