2013-10-11 80 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哈哈沒有問題。有時你只需要新的眼睛來解決問題。 –