Estoy tratando de crear un complemento jQuery que creará algo así como un autoCompleteBox pero con características personalizadas. ¿Cómo guardo las variables miembro para cada elemento jQuery coincidente?¿Cómo tener variables miembro y métodos públicos en un plugin jQuery?
Por ejemplo, tendré que almacenar un ID de tiempo para cada uno. También me gustaría almacenar referencias a algunos de los elementos DOM que componen el control.
Me gustaría ser capaz de hacer un método público que funciona algo así como:
$("#myCtrl").autoCompleteEx.addItem("1");
Pero en la implementación de addItem() ¿Cómo puedo acceder a las variables miembro de ese objeto particular, al igual que su timerId ¿o lo que sea?
A continuación se muestra lo que tengo hasta ahora ...
Gracias por cualquier ayuda o sugerencias!
(function($)
{
//Attach this new method to jQuery
$.fn.autoCompleteEx = function(options)
{
//Merge Given Options W/ Defaults, But Don't Alter Either
var opts = $.extend({}, $.fn.autoCompleteEx.defaults, options);
//Iterate over the current set of matched elements
return this.each(function()
{
var acx = $(this); //Get JQuery Version Of Element (Should Be Div)
//Give Div Correct Class & Add <ul> w/ input item to it
acx.addClass("autoCompleteEx");
acx.html("<ul><li class=\"input\"><input type=\"text\"/></li></ul>");
//Grab Input As JQ Object
var input = $("input", acx);
//Wireup Div
acx.click(function()
{
input.focus().val(input.val());
});
//Wireup Input
input.keydown(function(e)
{
var kc = e.keyCode;
if(kc == 13) //Enter
{
}
else if(kc == 27) //Esc
{
}
else
{
//Resize TextArea To Input
var width = 50 + (_txtArea.val().length*10);
_txtArea.css("width", width+"px");
}
});
}); //End Each JQ Element
}; //End autoCompleteEx()
//Private Functions
function junk()
{
};
//Public Functions
$.fn.autoCompleteEx.addItem = function(id,txt)
{
var x = this;
var y = 0;
};
//Default Settings
$.fn.autoCompleteEx.defaults =
{
minChars: 2,
delay: 300,
maxItems: 1
};
//End Of Closure
})(jQuery);
Eres un genio ... ¡Gracias a un millón por esto! – Legend