1
죄송 개체 jQuery를 액세스 할 필요 jQuery의 스크립트.. 내가 사과 그래서 그 문제에 대해 일반적으로 jQuery를 프로그래밍에 새로 온 사람
'초기화'방법의 범위 밖에있는 '설정'개체의 속성에 액세스 할 수 있어야합니다. '유효성 검사'라는 다른 메서드에서이를 유지하려고합니다. '나중에 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);