Я хочу передать как объект несколько вариантов для моего jQuery-плагина. Вот как я это делаю:Использование строки в качестве ссылки на объект
$("#contact-form").formValidate({
contactname: {
"minLength": 5
},
contactemail : {
"minLength": 4
},
contactsubject : {
"minLength": 10
},
contactmessage : {
"minLength": 25
}
});
В моей функции я хочу сослаться на объект шпагатом, который вводится идентификатор поля формы.
$.fn.formValidate = function(options) {
//...
var $this = $(this);
var id = $this.attr("id");
var length = options.id.minLength;
//...
}
Это решение не работает.
// редактировать
(function($, window, document, undefined) {
$.fn.formValidate = function(options) {
/*
* Deafults values for form validation.
*/
var defaults = {
minLength: 5,
type : "text",
required : true
};
var methods = {
error : function(id) {
$(id).css("border", "1px solid red");
}
}
var settings = $.extend({}, defaults, options);
console.log(options);
this.children().each(function() {
var $this = $(this);
var tagName = $this[0].nodeName;
var inputType = $(this).attr("type");
var id = $this.attr("id");
console.log(id);
var property = options[id].minLength;
if (tagName == "INPUT") {
console.log("property: " + property);
console.log("--------------------------");
$(this).keyup(function() {
if ($this.val().length > 0) {
$this.css("border", "1px solid red");
} else {
$this.css("border", "1px solid #ccc");
}
});
}
});
return this;
};
}(jQuery));
Когда я изменить 'опции [ID] .minLength' в' options.minLength [ID] 'это работает хорошо, но я также изменить' $ ("# контакт-форма"). FormValidate ({ \t \t CONTACTNAME: { \t \t \t MinLength: 5 \t \t}. 'к' \t $ ("# контакт-формы") formValidate ({ \t \t MinLength: { \t \t \t CONTACTNAME: 5 \t \t}, ' – cookieholic3
У вас есть опции. [id] .minLength' там. – JJJ
Когда я вставлял код здесь, я сделал опечатку, извините – cookieholic3