我一直在看这个错误的其他帖子,但在这种情况下似乎无法弄明白。我正在尝试保存到localstorage,但是我得到了这个错误,它说“radio”变量是未定义的,但它似乎不是。我也在使用JQMobile。这是小提琴:http://jsfiddle.net/KpbLD/第28行似乎是问题所在。
这是功能:
var getSelectedRadio = function(){
var radios = document.forms[0].acoustic;
for(var i=0, j=radios.length; i<j; i++){
if(radios[i].checked) {
console.log(acousticValue);
acousticValue = radios[i].value;
}
}
return acousticValue;
};
你无法读取undefined
的属性。它通常没有属性(参见http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/)。
所以,在你尝试阅读它的length
之前,请确保它没有未定义。或者它有一个length
,就像
if (obj.length) { your code }
这假设obj
不是undefined
。如果你也不确定,那么:
if (obj && obj.length) { your code }
document.forms[0].acoustic
不存在因此未定义。当您将无线电设置为该设备然后尝试查找长度时,它将失败。
您可能需要查找不同的对象,或者至少在尝试迭代之前检查对象是否存在。
请检查typeof variable(radios)
。这似乎是对象。如果它是一个数组,则不会出现此错误。