试图循环返回一些返回的JSON数据,如果返回的值分别为html元素attr,则将“checked”添加到复选框,但这会导致错误。
$('.varUserSettingTable tr').each(function(index, el) {
var $this = $(this);
$.each(curdata.VarUserDetails.VARS, function(index, value) {
if (curdata.VarUserDetails.VARS[index] !== null) {
if ($this.find('.SettingCheckbox').attr('name') === curdata.VarUserDetails.VARS[index].userID) {
$this.find('.SettingCheckbox').prop('checked', true);
}
}
});
});
抛出错误“Uncaught TypeError:无法读取未定义的属性'userID'”
在if statmenet上确认.userID!== null
VarUserDetails: Object
VARS:Array[1024]
[0 … 99]
0:null
1:null
2:Object
userChecked:1
userID:"2"
__proto__:Object
3:Object
userChecked:1
userID:"3"
__proto__:Object
...
...
答案 0 :(得分:1)
value
本身就是数组的项目。尝试使用value
代替curdata.VarUserDetails.Vars[index]
$('.varUserSettingTable tr').each(function(index, el) {
var $this = $(this);
$.each(curdata.VarUserDetails.VARS, function(index, value) {
if (value !== null) {
if ($this.find('.SettingCheckbox').attr('name') === value.userID) {
$this.find('.SettingCheckbox').prop('checked', true);
}
}
});