-1

当我用 for 循环循环这个数组时,它以某种方式给了我 undefined in

updateField(this.value, vehicles[i]+"_brand"

尽管

$("#"+vehicles[i]+"_year").change(function(){

确实获得了数组的正确值。这怎么可能,我该如何解决?

var vehicles = new Array();
    vehicles[0] = "auto";
    vehicles[1] = "truck";
    vehicles[2] = "motor";

for(var i = 0;i < vehicles.length;i++){
    $("#"+vehicles[i]+"_year").change(function(){
       updateField(this.value, vehicles[i]+"_brand", 1, 2, this.parentNode.id), resetBelow(0,'auto'), show('auto_brand');
    });
}
4

2 回答 2

1

将再次评估将触发更改事件的时间vehicles[i],您将获得最后一个值i.

改变

vehicles[i]+"_brand"

this.id.split('_')[0]+"_brand"
于 2013-02-05T09:27:54.797 回答
0

看起来你在我应该分号的地方有逗号:

updateField(this.value, vehicles[i]+"_brand", 1, 2, this.parentNode.id); resetBelow(0,'auto'); show('auto_brand');

...或者您的updateField()函数可能有更多参数,而您只是过早地关闭了该函数?

于 2013-02-05T09:29:54.383 回答