我一直对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