2014-09-03 169 views
1

我目前正在在基于JS-形式的处理器,需要表单字段和定义它们基于所述name=属性为多个级别的对象:的Javascript:访问多维对象属性

<input type="number" step="1" min="1" max="12" name="ui.house.occupant.adult.count"> 

使用form2js插件,它创建一个对象。在这种情况下,console.log显示的UI对象如下: http://i58.tinypic.com/2gy9duu.jpg

然而,每当我尝试任何事情,如alert(ui.house.occupant.adult.count)访问我的JS代码这个属性,它抛出的错误house是不确定的。你可以看到:http://i59.tinypic.com/2guz7s7.jpg

我在绞尽脑汁想弄清楚问题在哪里。当我使用alert(ui.house)时,它会返回空白警报。 这导致我认为form2js在我尝试访问它之前实际上没有正确处理表单数据,但是我试图使用诸如setTimeout之类的东西无济于事。

对于上下文,我加载了一个外部CSV文件,该文件预填充了用户可以选择的数据的表单字段,因此为什么第二个屏幕截图显示在$ .ajax函数内部。

谢谢!

编辑:的代码更完整的示例:

<fieldset id="user_input"> 
<div class="question_row"> 
    <label for="ui.house.ng.available">Is natural gas available in your street?</label> 
    <div class="question"> 
     <input type="radio" name="ui.house.ng.available" value="True"> Yes 
     <input type="radio" name="ui.house.ng.available" value="False"> No 
     <input type="radio" name="ui.house.ng.available" value="False"> Don't know 
    </div> 
</div> 
<div class="question_row"> 
    <label for="ui.house.occupant.adult.count">How many people live in your home?</label> 
    <div class="spec_people question"> 
     Adults: 
     <input type="number" step="1" min="1" max="12" name="ui.house.occupant.adult.count"> 
     Children: 
     <input type="number" step="1" min="1" max="12" name="ui.house.occupant.child.count"> 
    </div> 
</div> 
</fieldset> 

而完整的Ajax功能 - 这拉动外部CSV并填充<option>元件表单字段以及限制属性。

var ui = new Object; 

// Load our external spreadsheet containing constants, questions, and other definitions. Format it nicely. 
$.ajax({ 
    url: "DSD_Master_Constants.csv", 
    async: false, 
    dataType: "text", 
    success: function (csvd) { 
     data = $.csv2Array(csvd); 
        // call a function on complete 
     $.each(data, function(index, rows){ 
      // 0 Constant Type 
      // 1 Constant category 
      // 2 Constant subcategory   
      // 3 
      // 4 
      // 5 
      // 6 
      // 7 Value (min) 
      // 8 Value (max) 
      // 9 Custom option? (separate with comma) 
      // 10 Custom option value (same sequence) 
      // 11 Unit of measurement 
      // 12 Field type 
      // 13 Helpful tip 
      // 14 Object code 
      if(rows[0] == 'User'){ // Populate User Input Fields 
       var inputf = $("[name='"+rows[14]+"']"); 
       var htmldat = ''; 
       switch(rows[12]){ 
        case 'number': 
         inputf.attr({ 
          value: rows[7], 
          placeholder: rows[7], 
          min: rows[7], 
          max: rows[8] 
         }); 
         break; 
        case '': // must be text field 
         break; 
        case 'select': 
         if(rows[9] == ''){ // No custom option, means use min and max values 
          var minno = rows[7]; 
          var maxno = rows[8]; 

          while(minno <= maxno){ 
           htmldat += '<option value="' + minno + '">' + minno + ' ' + rows[11] + '</option>  \ 
           '; 
           minno = parseFloat(minno) + 1; 
          } 
          inputf.html(htmldat); 
         } else { // Must be using a custom option/value set 
          var custom = rows[9].split(', '); 
          var customv = rows[10].split(', '); 
          for(var a in custom){ 
           if(customv[a] == ''){ customv[a] = custom[a]; } 
           htmldat += '<option value="' + customv[a] + '">' + custom[a] + '</option> \ 
           '; 
          } 
          inputf.html(htmldat); 
          htmldat = ''; 
         } 
         break; 
        case 'radio': 

         break; 
       } 

      } 
     }); 
     ui = $("#user_input").toObject(); // Splits form data into objects and sub-objects, based on delimiter (in this case, period '.') 
     console.log(ui); 
    }, 
    complete: function() { 
     alert(ui.house.occupant.adult.count); 
    } 
}); 

我希望这是有道理的 - 我试图简化它,以节省人们不得不通过我的整个代码 - 我的坏!

+0

请给你的代码的更完整的例子:看http://stackoverflow.com/help/mcve;一种方法是把你的代码放在jsfiddle中。 也请使用代码块,而不是截图。 猜测这是一个范围问题,但直到显示大图时才能分辨出来。 (尤其是声明UI对象的地方) – 2014-09-03 02:05:50

+0

ui的变量范围是什么? – Igor 2014-09-03 02:23:45

+0

'ui'变量是一个全局对象集,将在当前页面的其他函数中使用 - 希望能回答你的问题。 – 2014-09-03 02:29:05

回答

1

当你做了ui = $(“#user_input”)。toObject(); 用户界面成为您的用户界面对象中的一个字段。

要访问您需要做ui.ui.house的房子。

仔细看看form2js回报

+0

你是上帝。非常感谢 - 最近两天我花了我的大脑 - 我想要解决这个问题,我需要做的就是删除'ui'。在每个输入字段的开始处。但是,我已经有了基于现有名称的函数和选择器,所以我刚刚将'ui'对象重新定义为'ui = ui.ui;'。真诚地谢谢你。 – 2014-09-04 05:50:17

0

在您提供的HTML,有一个与在你的代码中引用“USER_INPUT”的ID不输入元素:

ui = $("#user_input").toObject(); // Splits form data into objects and sub-objects, based on delimiter (in this case, period '.') 

也许你并没有包括所有的HTML,但你”已经提供,看起来你正在尝试访问一个不存在的元素。

+1

整个表单被封装在一个'

'中,我忘了将它包含在我的HTML代码片段中。抱歉! – 2014-09-03 02:27:04