博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
simple jquery auto completed plugin
阅读量:7003 次
发布时间:2019-06-27

本文共 1647 字,大约阅读时间需要 5 分钟。

  hot3.png

jquery-simpleAutoCompleted.js

简单过滤,并显示列表.

development v0.2

1. 返回一个新实例

2. 接受一个外部数组数据

3. 内部封装

4. 提供接口

(function($) {	// default settings:	var defaults = {		id: false,		resultPanel : false,		data : [],			};	var MyAutoCompleted = function(element, options) {		var elem = $(element);		var obj = this;								this.config = $.extend( {}, defaults, options );				// keyup handler	    this._input = function(e) {    		    	var txt = elem.val(), kc = e.keyCode;    	    		    	$("#"+obj.config.resultPanel).html("");	    		    	if (e && (kc === 13 || kc === 38 || kc === 40)) {				return;			}    		    		    	if (txt) {    			    		setTimeout(function() {	    			obj._build(obj._regSearch(txt));	    		},500);    			    	} else {	    		obj._build(obj.config.data.slice(0, 4));	    	}     		    }  	    /**	     * create ui of template results	     */	    this._build = function(items) {	    	var li = "";	    	$.each(items, function(i, n) {	    		li += "
  • "+n+"
  • "; }); $("#"+obj.config.resultPanel).html(li); } /** * filter results from data */ this._regSearch = function(k) { var match = []; $.each(obj.config.data, function(i, n){ if (n.indexOf(k) != -1) { match.push(n); } }); return match; } }; $.fn.myAutoCompleted = function(options) { return this.each(function() { var element = $(this); var myplugin = new MyAutoCompleted(this, options); element.unbind("keyup").bind("keyup", myplugin._input); }); };})(jQuery);

    demo.html

    	Simple Autocomplete		

      转载于:https://my.oschina.net/ym80/blog/220707

      你可能感兴趣的文章