开发者

Jquery create array problem with for

Hello I have a prboblem with the next code:

function loadOptions(num){
listTabs = new A开发者_开发知识库rray();
for(var i = 1 ; i < parseInt(num) + 1 ; i++){
    var tabActu = {
        'name':'tab'+i,
        'src':'urlImatge'
        };
    listTabs.add(tabActu);
    $.each(listTabs,function(key,value){
        alert(key+" : "+value);
    });
}

}

I need to create an list of elements equal to the num parameter. I can't find the error.


Did you look in the error console for javascript errors?

Javascript arrays don't have a .add() method. You can use .push().

function loadOptions(num){
    listTabs = new Array();
    var len = parseInt(num, 10);
    for (var i = 1 ; i < len + 1 ; i++) {
        var tabActu = {
            'name':'tab' + i,
            'src':'urlImatge'
            };
        listTabs.push(tabActu);
        $.each(listTabs,function(key,value){
            alert(key+" : "+value);
        });
    }
}

In addition to change to .push(), parseInt must always be passed the radix value and you should remove the function call to parseInt from the loop so it's not called on every iteration. Also, you haven't delcared listTabs here so that makes it a global variable. Is that what you intended?


Sup Francesc

Arrays dont have a add method ..... use push

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜