cities.statename.data generate error无法读取Object.city中未定义的属性“data”

时间:2017-02-28 06:24:53

标签: javascript jquery html

  

无法读取Object.city中未定义的属性“数据”

问题是我在动态地调用状态,当我编写cities.UP.data时它显示正确的结果,当我添加cities.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>

1 个答案:

答案 0 :(得分:0)

我认为你想在这一行中做的是引用城市中的州名。

return document.getElementById("slectstate2").innerHTML = printHtmlAttr.option(cities.statename.data);

这会引发错误。

尝试下面一个。

return document.getElementById("slectstate2").innerHTML = printHtmlAttr.option(cities[statename].data);