2012-01-05 245 views
0

我覺得我在用下面的代碼呼叫功能不工作

我試圖調用雙向的功能,但這麼想的工作第二種方式this元素一個問題:

函數validate_email沒有響應,我想在第二個例子中調用它是錯誤的。

的工作方式是:

checking = {}; 
checking.form = function(){ 
    return{ 

validate_mail: function(a){ 
    var c=true; 
    var email_filter = /^[\w.-][email protected][\w.-]+([\.]+([\w]+[\.])?)+[a-zA-Z]{2,6}$/i; 
    if($.trim(a)!="" && !$.trim(a).match(email_filter)) 
    c=false; 
    return c; 
}, 
check_mail: function(div_id, error) { 

     var email = $(div_id + ' input[name=email]').val(); 
     var check = this.validate_mail(email); 
     if(check==false && $(div_id).is(':visible')) 
     { 
      $(div_id + ' '+ error).html('<b>some error</b>'); 
     } else { 
      $(div_id + ' ' + error).text(''); 
     } 

    } 
} 
}(); 


$(function(){ 
    $('#register input[name=email]').blur(function(){ 
     checking.form.check_mail("#register", ".error_email"); 
     }); 
} 

的不工作的方式是:

checking = {}; 
checking.form = function(){ 
    return{ 

validate_mail: function(a){ 
    var c=true; 
    var email_filter = /^[\w.-][email protected][\w.-]+([\.]+([\w]+[\.])?)+[a-zA-Z]{2,6}$/i; 
    if($.trim(a)!="" && !$.trim(a).match(email_filter)) 
    c=false; 
    return c; 
}, 
check_mail: function(div_id, error) { 
      return function(){ //**made the change nr. 1** 

     var email = $(div_id + ' input[name=email]').val(); 
     var check = this.validate_mail(email); 
     if(check==false && $(div_id).is(':visible')) 
     { 
      $(div_id + ' '+ error).html('<b>some error</b>'); 
     } else { 
      $(div_id + ' ' + error).text(''); 
     } 
     } 
    } 
} 
}(); 


$(function(){ //**made the change nr. 2** 
    $('#register input[name=email]').blur( 
     checking.form.check_mail("#register", ".error_email") 
     ); 

} 

我需要一些幫助與此有關。我相信我沒有正確使用this元素

任何幫助表示讚賞!

+1

究竟是不是工作? – Stefan 2012-01-05 21:28:10

+0

如果我們甚至不知道代碼應該做什麼以及想要做什麼,我們如何能夠幫助您? – 2012-01-05 21:29:06

+0

函數validate_email沒有響應,我認爲在第二個例子中調用它是錯誤的 – sorin 2012-01-05 21:31:31

回答

1

當您返回匿名函數時,this未引用所有者對象。你可以在某些變量this值存儲在返回函數之前,並使用它,而不是這樣的:

... 
var that = this; 

return function(){ 
    that.validate_mail(...); 
} 
... 
2

而不是使用

this.validate_mail(email);

使用

checking.form.validate_mail(email);它應該工作的罰款。

您的案例中不能使用this,因爲您沒有創建實例。

+0

checking.form.validate_mail(在你的答案中的小錯字) – JohnJohnGa 2012-01-05 21:36:02

1

在我看來,你的編碼方式是升技奇怪的,很容易犯錯誤,這是也是錯誤所在。 this在check_mail函數內的匿名函數內部是不知道的。

我將重寫代碼,如下,以使其更清晰:

var checking = { 
    form: { 
     validate_mail: function(a){ 
      var c=true; 
      var email_filter = /^[\w.-][email protected][\w.-]+([\.]+([\w]+[\.])?)+[a-zA-Z]{2,6}$/i; 
      if($.trim(a)!="" && !$.trim(a).match(email_filter)) 
       c=false; 
      return c; 
     }, 
     check_mail: function(div_id, error) { 
      var email = $(div_id + ' input[name=email]').val(); 
      var check = this.validate_mail(email); 
      if(check==false && $(div_id).is(':visible')) 
      { 
       $(div_id + ' '+ error).html('<b>some error</b>'); 
      } else { 
       $(div_id + ' ' + error).text(''); 
      } 
     } 
    } 
} 

用法:

$('#register input[name=email]').blur(function() { 
    checking.form.check_mail("#register", ".error_email"); 
}); 
+0

函數的調用在這種情況下應該怎麼看? – sorin 2012-01-05 21:54:11

+0

因此:'$('#register input [name = email]')。blur(function(){checking.form.check_mail(「#register」,「.error_email」);});' 注意額外匿名功能。 而不是創建返回可用於綁定的函數的函數,而是創建可執行的函數,並從綁定到事件時使用的匿名函數調用它們。 – jeffreydev 2012-01-05 22:09:40