0

我一直坚持让 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);
            }


        }


    })();

    })
4

2 回答 2

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);
                }
            }
        })();
   });
于 2013-08-03T03:05:14.663 回答
0

放置这条线

var levelSelected = $('.gameLevel :selected')

里面

newGame : function (){
   var levelSelected = $('.gameLevel :selected');
   var level =levelSelected.val();
   console.log('Doin level #: ' + level);
}
于 2013-08-03T03:00:32.473 回答