0

我想要做的是在for(). 现在它看起来像这样,但据我所知,你不能从这样的变量运行命令,我不能运行, (eg) cab_type_value = "whatever"。我也不能跑road_load_enabled;它只是行不通。有谁可以做到这一点?如何解析我的Jvar document.MyForm.InputName.disabled

for( var j=1; j<=14; j++ ) {
    var cab_type_value = "document.exe_mode_form.cab_type" + j + ".value";
    var cab_type_checked = "document.exe_mode_form.cab_type" + j + ".checked == 1";

    for( var i=1; i<=document.exe_mode_form.road_load_number.value; i++ ) {
        var road_load_value = "document.exe_mode_form.load" + i + ".value";
        var road_load_enabled = "document.exe_mode_form.load" + i + ".disabled = false";
        var road_load_disabled = "document.exe_mode_form.load" + i + ".disabled = true";
        var rld_db = "document.exe_mode_form.a" + i + "_a1.value";

        if ( cab_type_checked ) {
            if ( test == 1 ) {
                if(road_load_disabled) {
                    alert("road_load_disabled");
                    road_load_enabled;
                }
                test = 2;
            }

            if(cab_type_value == rld_db) {
                if(olof == 1) {
                    alert("cab_type_value  == rld_db");
                    olof = 2;
                }

                road_load_enabled;
            }
        }
    }
}

此外,这部分不起作用:

if(cab_type_value == rld_db) {
    if(olof == 1){
        alert("cab_type_value  == rld_db");
        olof = 2;
    }

    road_load_enabled;
}

我已经检查过,与 .cab_type_value具有相同的值rld_db

4

1 回答 1

1

尝试更改这些行:

var cab_type_value = "document.exe_mode_form.cab_type" + j + ".value";

至:

var cab_type_value = document.exe_mode_form['cab_type' + j].value;

同样的逻辑也适用于函数调用:

someVariable = 'alert';
window[someVariable]('Foo!');//alerts foo

如果你的函数没有在全局范围内声明,你可以window用任何命名空间对象替换:foobarObject.orEven.nestedOnes[someVariable]();
最后,因为我个人讨厌这个window关键字(它是一个循环引用),你可以简单地选择this['alert']('foo');在常规函数调用或全局中使用范围。this指向其调用的上下文,在这些情况下是全局对象

于 2012-07-19T15:00:34.600 回答