无法读取Object.city上未定义的属性“数据”
问题是当我编写citys.UP.data时,它使用正确的状态来动态调用状态,当我添加citys.statename.data时,这里的状态名称会在我选择任何状态时动态生成。person.details.getInfo.city($(this).val()); city : function (statename)
<!DOCTYPE html>
<html>
<head>
<script src="https:ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<title></title>
<script type="text/javascript">
var states = ['UP','MP','MH','DL' ];
var cities = {
"UP":{
data:['allahabad','kanpur','luckhnow']
},
"MP":{
data:['Bhopal','Jhansi','Gwalior']
}
};
var area = [
alld = ['kr','kp','mu']
];
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue",
details :{
getInfo :{
state : function ()
{
return document.getElementById("slectstate").innerHTML = printHtmlAttr.option(states);
},
city : function (statename)
{
statename = statename.trim();
console.log(statename);
return document.getElementById("slectstate2").innerHTML = printHtmlAttr.option(cities.statename.data);
},
area : function (city)
{
},
}
}
}
var printHtmlAttr={
option : function(val)
{
var options='<option value="">choose</option>';
$.each(val,function(e,v){
options = options+'<option value="'+v+'">'+v+'</option>';
});
return options;
}
}
$(document).ready(function(){
person.details.getInfo.state();
$("body").on("change","#slectstate",function(){
person.details.getInfo.city($(this).val());
});
});
</script>
</head>
<body>
<select id="slectstate"></select>
<select id="slectstate2"></select>
</body>
</html>
最佳答案
我认为您想在此行中引用的是城市的州名。
return document.getElementById("slectstate2").innerHTML = printHtmlAttr.option(cities.statename.data);
这将引发错误。
请尝试以下一项。
return document.getElementById("slectstate2").innerHTML = printHtmlAttr.option(cities[statename].data);