【发布时间】:2014-02-28 14:14:24
【问题描述】:
我有带有键值和对的对象列表。当我将它们分开以在特定字段上显示每个信息时。 我收到类型错误 data.validation[i].user is undefined 但我检查了变量是否已被赋值。
var data = {validation:[
{"user":"user may not be empty"},
{"date":"Date may not be null"},
{"mobile":"passengerMobile may not be empty"},
{"mobileSize":"passengerMobile size must be greater than 11"},
{"name":"passengerName may not be empty"},
{"nameSize":"passengerName size must be between 2 and 30"},
]};
var size = data.validation.length;
for(var i =0;i<=size;i++){
if(data.validation[i].user){
$("#username").html("<p>"+data.validation[i].user+"</p>");
}
if($("#mobile").val().length == 0){
$("#mobilesize").html("<p>"+data.validation[i].mobile+"</p>");
}
else if($("#mobile").val().length >= 1){
$("#mobilesize").html("<p>"+data.validation[i].mobilesize+"</p>");
}
}
$("#mobile") 表示输入字段Id
热烈欢迎任何想法
【问题讨论】:
-
javascript数组索引从0开始到length-1;
-
谢谢@xff1874。我知道那个。但我错过了那个东西
标签: javascript undefined keyvaluepair object-properties