开发者

Problem with document.forms[i][j].type, returns undefined

开发者 https://www.devze.com 2023-03-08 18:02 出处:网络
I\'m writing a firefox addon. Here is a part of the quote var cForm = \'\'; var doc = document; for (i = 0; i < doc.forms.length; i++)

I'm writing a firefox addon. Here is a part of the quote

var cForm = ''; 
var doc = document;
for (i = 0; i < doc.forms.length; i++)
{
    var a = doc.forms[i].name + "";
    if ( a.search("itsMe") != -1)
    {
        cForm = i;
    }
}

//

if ( cForm != '' )
{
    for (i = 0; i < doc.forms[cForm].length; i++)
    {
        var sTotal = 'doc.forms[' + cForm + '][' + i +'].type';             
        if ( eval(sTotal) == "button")
        {
            return sTotal ;
        } 

    }
}

The first code works well. The 2nd code sho开发者_如何转开发ws an error like: "doc.forms[0][0] is undefined"

whats wrong?? help...


The issue could be in your use of eval; it might not know about doc, since it's not in scope. In general, you should try to avoid using eval. You really don't need it in this case, so try doing something like this:

if (i.length && typeof(cForm) != "undefined")
{
    cForm = parseInt(cForm);
    for (i = 0; i < document.forms[cForm].length; i++)
    {
        if (document.forms[cForm][i].type == "button")
        {
            return sTotal ;
        } 

    }
}


doc.forms[0][0] could be a textarea, say, which would make it have no .type.

On a separate note, why are you using eval, exactly???

0

精彩评论

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