首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >可以从Javascript访问JSON属性

可以从Javascript访问JSON属性
EN

Stack Overflow用户
提问于 2018-08-10 00:35:20
回答 1查看 53关注 0票数 -1

我的json:

代码语言:javascript
复制
     {
  locale: "en",
  title: "Survey",
  focusFirstQuestionAutomatic: false,
  pages: [
   {
    name: "livingEnvironment",
    elements: [
     {
      type: "html",
      name: "navigationWarning",
      html: "To navigate "
     },
     {
      type: "html",
      name: "IntroEnvironment",
      html: "We will now ask you questions about your living environment"
     },
     {
      type: "text",
      name: "numhousehold",
      width: "auto",
      title: "How many people (including yourself) lived in your household",
      validators: [
       {
        type: "numeric",
        text: "Please enter a number between 1 and 99.",
        minValue: 1,
        maxValue: 99
       }
      ],
      inputType: "number"
     },
     {
      type: "multipletext",
      name: "householdtype",
      width: "auto",
      title: "Of these, how many (including yourself) were:",
      items: [
       {
        name: "children",
        inputType: "number",
        title: "Children under 18 years old",
        validators: [
         {
          type: "regex",
          text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
          regex: "^(\\s*|\\d+)$"
         }
        ]
       },
       {
        name: "adults",
        inputType: "number",
        title: "Adults between 18-59 years old",
        validators: [
         {
          type: "regex",
          text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
          regex: "^(\\s*|\\d+)$"
         }
        ]
       },
       {
        name: "seniors",
        inputType: "number",
        title: "Seniors (60+)",
        validators: [
         {
          type: "regex",
          text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
          regex: "^(\\s*|\\d+)$"
         }
        ]
       },
       {
        name: "disabled",
        inputType: "number",
        title: "Disabled",
        validators: [
         {
          type: "regex",
          text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
          regex: "^(\\s*|\\d+)$"
         }
        ]

      }
   }
 ]}

我的javascript是:

代码语言:javascript
复制
function serverValidateQuestion(survey, options) {
            console.log('Validation called');
            console.log(options.data.householdtype);
            console.log(options.data.householdtype.children);

所以我可以访问console.log(options.data.householdtype),它给我提供了in type类型中的项目列表。然而,我正在尝试访问从householdtype输入的"children“值。当我写console.log(options.data.householdtype.children)的时候,它给了我

代码语言:javascript
复制
`

无法读取“未定义”错误的属性。在谷歌控制台模式下,"console.log(options.data.householdtype)“为我提供了以下信息:

`“成人:"3”儿童:"3“残疾:"3”孕妇:"3“老年人:"3”原件:Object

但我只想访问孩子的值,而console.log(options.data.householdtype.children)不起作用。此外,console.log(options.data.numhousehold)确实给了我一个用户输入的值。此外,这些值来自用户在调查期间填写的输入框。请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2018-08-10 04:16:47

好的,我想出来了,因为在我的html文件console.log(options.data.householdtype)的第一页上,给出了未定义的。在我的html的第二页上,它是否找到了householdtype,然后它就可以访问console.log(options.data.householdtype.children);了,否则它就不能工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51772062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档