2012-06-22 55 views
1

對不起,我是新來的jQuery和編程一般爲此事如此道歉,如果我的問題是有點啞..需要訪問對象的jQuery

我試圖寫一個快速和骯髒的表單驗證在jQuery中的腳本

我需要能夠從'init'方法的範圍之外訪問我'設置'對象中的屬性,我試圖在另一個我稱之爲'validate '我不能僅僅將對象作爲參數傳遞給validate方法,因爲我需要稍後調用我的validate方法。這是我的腳本:

(function($) { 
    var methods = { 
     init: function(options) { 

      var settings = $.extend({ 
       bgColor: '#fff', 
       textType: 'normal', 
       textColor: '#666', 
       errorMsgClass: 'errorMsg', 
       requiredMsgClass: 'requiredMsg', 

       ex: { 
        'email': /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/, 
        'message': /^[0-9a-zA-Z ."']+$/, 
        'name': /^[0-9a-zA-Z,\r\n ."']+$/, 
        'phone': /^[0-9 ]+$/, 
        'url': /^[0-9a-zA-Z,\r\n ."']+$/ 
       } 
      }, options) 

      return this.each(function() { 
       var o = settings; 
       var obj = $(this); 
       var items = $(":input", obj); 
       items.each(function() { 

        if (this.parentNode.className.substr(0, 8) == 'validate') { 

         $(this).bind({ 
          focus: function() { 
           if ($(this).val() == $(this).attr('id')) { 
            $(this).val(''); 
           } 
           methods.msgHide($(this), '.errorMsg') 
          }, 
          blur: function() { 

           methods.validate($(this)) 

          } 
         }); 
        } 
       }); 

      }); 

      return settings; 
     }, 
     validate: function(el) { 

      if (el.val() == '') { 

       methods.is_required ? methods.msgShow(el, '.requiredMsg') : ''; 
       el.val(el.attr('id')) 

      } 
      else { 
       //hide required msg 
       methods.msgHide(el, '.requiredMsg'); 
       var last = methods.get_length(el); 
       var reg = methods.getWrapper(el).attr('class').substr(9, last); 
       if (!el.val().match(methods.init.ex[reg])) { 
        methods.msgShow(el, '.errorMsg') 
       } 
      } 
     }, 
     get_length: function(el) { 

      //see if it has the trailing asterix 
      if (methods.is_required) { 
       return el.closest('div').attr('class').length - 10; 
      } 
      else { 
       return el.closest('div').attr('class').length - 9; 
      } 
     }, 
     is_required: function(el) { 

      //see if it has the trailing asterix 
      if (el.closest('div').attr('class').lastIndexOf('*') > -1) { 
       return true 
      } 
      else { 
       return false 
      } 
     }, 
     getWrapper: function(el) { 

      return el.closest('div'); 

     }, 
     msgShow: function(el, message) { 
      methods.getWrapper(el).children(message).css({ 
       display: "block" 
      }); 
     }, 

     msgHide: function(el, message) { 
      methods.getWrapper(el).children(message).css({ 
       display: "none" 
      }); 
     }, 
    }; 

    $.fn.validateForm = function(method) { 

     // Method calling logic 
     if (methods[method]) { 
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
     } else if (typeof method === 'object' || !method) { 
      return methods.init.apply(this, arguments); 
     } else { 
      $.error('Method ' + method + ' does not exist on jQuery.validateForm'); 
     } 

    }; 

})(jQuery); 

回答

0

您可以將設置對象傳遞給需要它的任何函數。

functionThatNeedsSettings(settings);