2013-10-11 73 views
0

我有一个jQuery UI对话框,并且我试图在对话框中按下按钮时验证输入。'TypeError:t.validation is undefined',但它似乎被定义

但是,当我点击“添加”按钮并告诉我TypeError: t.validation is undefined时,它失败。据我所知它是定义的(在init函数中)。

我怀疑我把它设置错了,但我看不到在哪里寻找。有人可以告诉我如何解决这个错误?

$(document).ready(function(){ 
    optionsDialogFuncs.init(); 
}); 

optionsDialogFuncs = { 

    /** 
    * Constructor 
    */ 
    init : function(){ 

     var t = this; // This object 

     $('#add_option').on('click', function(){ 

      /** Create an object to hold the result of validation checks and the final result */ 
      t.validtaion = {}; 
      t.add_option(); 

     }); 

    }, // init 

    /** Add a single option to the list of predefined options */ 
    add_option : function(){ 

     var t = this; // This object 
      tip = $('.validation-tip', '#add-remove-options-form'); 

     t.validate_add_option(); 

     /** Check to see if the option was valid */ 
     if(t.validation.valid !== true){ 

      /** Output a validation tip to help the user */ 
      if(t.validation.exists === false){ 
       tip.text('Please enter an option.'); 
      } else if(t.validation.exists === false){ 
       tip.text('This option already exists.'); 
      } else{ 
       tip.text('An unknow validation error occured. Please try again.'); 
      } 

     } else{ 
      // Append input to list of options 
      // Update the 'poll_options' textarea 
      // Update the dialog display 
     } 

    }, // add_option 

    validate_add_option : function(){ 

     var t = this,         // This object 
      new_option = $('#new_option').val(),  // The new option that the user wishes to add 
      options_text = $('#poll_options').text(), // The text form the predefined options textarea, i.e. any existing options 
      options = options_text.split("\n");   // The predefiend options as an array 

     /** Check to see if this option already exists */ 
     t.validation.empty = (new_option === '') ? true : false; 

     /** Check to see if this option already exists */ 
     t.validation.exists = $.inArray(new_option, options); 

     /** Ensure the option passed all validity checks */ 
     t.validation.valid = (t.validation.empty === false && t.validation.exists === -1) ? true : false; 

    } // validate_add_option 

}; 
+0

此问题似乎是偏离主题,因为它是关于类型错误。 – iConnor

+0

这是一个编程问题。这不正是这个网站是关于什么?! –

+0

这是关于其他人真正有用的问题。这是它的主要目的,而不是调试你的代码 – iConnor

回答

4

您拼错了变量。

// this 
t.validtaion = {}; 
// should be 
t.validation = {}; 
+1

谢谢你,我看不到那个看!谢天谢地,周末从15分钟开始! –

+0

@DavidGard哈哈没有问题。有时你只需要新的眼睛来解决问题。 –