我开始坚持让newGame函数运行,我不知道是什么阻止了它。
我有一个包含3个级别的选择框,但是每次运行newGame函数时,它都会达到所选级别,它总是会返回1级,即使我有一个值为3的选择框。
我无法弄清楚为什么这不起作用?最令人抓狂的部分是我之前有这个工作,但不知何故,我做了一些阻止它工作的东西。任何帮助,我很感激!我也对代码结构的一般批评持开放态度。 谢谢!
$(document).ready(function(){
(function (){
var levelSelected = $('.gameLevel :selected'),
newGameSelected = $('.start');
newGameSelected.on('click', function() { abc.newGame(); })
var abc = {
level : levelSelected.val(),
abcArray : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'],
createLevel : function(){
console.log('test');
},
newGame : function (){
var level = this.level;
console.log('Doin level #: ' + level);
}
}
})();
})
答案 0 :(得分:0)
放置此行
var levelSelected = $('.gameLevel :selected')
内
newGame : function (){
var levelSelected = $('.gameLevel :selected');
var level =levelSelected.val();
console.log('Doin level #: ' + level);
}
答案 1 :(得分:0)
问题是您只在页面加载时获得$('.gameLevel :selected')
。当您更改选择并单击.start
时,此选择不会刷新。要解决此问题,您必须在每次点击$('.gameLevel :selected').val()
时重新获得.start
。像这样:
$(document).ready(function(){
(function (){
var newGameSelected = $('.start');
newGameSelected.on('click', function() {
abc.level = $('.gameLevel :selected').val();
abc.newGame();
})
var abc = {
abcArray : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'],
createLevel : function(){
console.log('test');
},
newGame : function (){
var level = this.level;
console.log('Doin level #: ' + level);
}
}
})();
});
或者
$(document).ready(function(){
(function (){
var newGameSelected = $('.start');
newGameSelected.on('click', function() {
abc.newGame($('.gameLevel :selected').val());
})
var abc = {
abcArray : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'],
createLevel : function(){
console.log('test');
},
newGame : function (level){
console.log('Doin level #: ' + level);
}
}
})();
});