【发布时间】:2011-02-06 18:11:52
【问题描述】:
标记:
<input type="text" name="email" />
代码:
$(':text').focusout(function(){
$(this).validate(function(){
$(this).attr('name');
});
});
插件:
(function($){
$.fn.validate = function(type) {
return this.each(function(type) {
if (type == 'email') {
matches = this.val().match('/.+@.+\..{2,7}/');
(matches != null) ? alert('valid') : alert('invalid');
}
/*else if (type == 'name') {
}
else if (type == 'age') {
}
else if (type == 'text') {
}*/
else {
alert('total failure');
}
});
};
})(jQuery);
问题是当我执行上面的代码时,它运行插件就像 type 是一个字符串:“function(){ $(this).attr('name'); });" 而不是将其作为函数执行。我该如何解决这个问题?
【问题讨论】:
-
调用 $(this).validate($(this).attr('name')) ?另外,使用 $(this).val() 而不是 this.val()。
标签: javascript jquery function types