您好,欢迎访问一九零五行业门户网

js实现select组件的选择输入过滤代码_javascript技巧

实现select组件的选择输入过滤作用的js代码如下:
/***其中//******之间的部分显示的是在没有选择输入过滤功能的代码上加入的功能代码**//** * @description this plugin allows you to make a select box editable like a text box while keeping it's select-option features* @description no stylesheets or images are required to run the plugin** @version 0.0.1* @author martin mende* @license attribution-noncommercial 3.0 unported (cc by-nc 3.0)* @license for comercial use please contact me: martin.mende(at)aristech.de* * @requires jquery 1.9+** @class editableselect* @memberof jquery.fn** @example** var selectbox = $(select).editableselect();* selectbox.addoption(i am dynamically added);*/(function ( $ ) {$.fn.editableselect = function() {var instancevar;//此this.each()指的就是对当前对象的遍历,这里的当前对象指代的就是对当前两个下拉选择框对象的一一遍历this.each(function(){var originalselect = $(this);//check if element is a selectif(originalselect[0].tagname.touppercase()===select){//wrap the original select在原始的外围插入
框originalselect.wrap($());var wrapper = originalselect.parent();wrapper.css({display: inline-block});//place an input which will represent the editable select//在选择菜单上加入input输入框var inputselect = $().insertbefore(originalselect);//get and remove the original idvar objid = originalselect.attr(id);originalselect.removeattr(id);//add the attributes from the original select//input输入框的各种属性设置inputselect.attr({alt: originalselect.attr(alt),title: originalselect.attr(title),class: originalselect.attr(class),name: originalselect.attr(name),disabled: originalselect.attr(disabled),tabindex: originalselect.attr(tabindex),id: objid});//get the editable css properties from the selectvar rightpadding = 15;inputselect.css({width: originalselect.width()-rightpadding,height: originalselect.height(),fontfamily: originalselect.css(fontfamily),fontsize: originalselect.css(fontsize),background: originalselect.css(background),paddingright: rightpadding});inputselect.val(originalselect.val());//add the triangle at the rightvar triangle = $().css({height: 0, width: 0,borderleft: 5px solid transparent,borderright: 5px solid transparent, bordertop: 7px solid #999,position: relative,top: -(inputselect.height()/2)-5,left: inputselect.width()+rightpadding-10,marginbottom: -7px,pointerevents: none}).insertafter(inputselect);//create the selectable list that will appear when the input gets focus//聚焦的时候加上下拉框var selectlist = $().css({display: none,liststyletype: none,width: inputselect.outerwidth()-2,padding: 0,margin: 0,border: solid 1px #ccc,fontfamily: inputselect.css(fontfamily),fontsize: inputselect.css(fontsize),background: #fff,position: absolute,zindex: 1000000}).insertafter(triangle);//add options//在resourcedata变量中存储当前下拉框中的所有数据//******var resourcedata = [];originalselect.children().each(function(index, value){prepareoption($(value).text(), wrapper);resourcedata.push($(value).text());});//******//bind the focus handler//在鼠标聚焦的时候fadein(100),即下拉显示当前下拉框inputselect.focus(function(){selectlist.fadein(100);//v存储的是在input输入框中输入的内容,如果输入的内容不为空,就在存储原始下拉框的所有数据中找到出现v中字符的数据压入newresourcedata变量中//******var v = inputselect.val();var newresourcedata = [];if(v != ) {$.each(resourcedata, function(i, t){if(t.indexof(v) != -1)newresourcedata.push(t);});}wrapper.children(ol).empty();$.each(newresourcedata, function(i, t){prepareoption(t, wrapper);});//******}).blur(function(){selectlist.fadeout(100); }).keyup(function(e){if(e.which==13) inputselect.trigger(blur);//在enter快捷键按下后弹起的时候执行的事件//******var v = inputselect.val();var newresourcedata = [];if(v != ) {$.each(resourcedata, function(i, t){if(t.indexof(v) != -1)newresourcedata.push(t);});}wrapper.children(ol).empty();$.each(newresourcedata, function(i, t){prepareoption(t, wrapper);});//******});//hide original elementoriginalselect.css({visibility: hidden, display: none});//save this instance to return itinstancevar = inputselect}else{//not a selectreturn false;}});//-end each/** public methods **//*** adds an option to the editable select* @param {string} value - the options value* @returns {void}*/instancevar.addoption = function(value){prepareoption(value, instancevar.parent()); };/*** removes a specific option from the editable select* @param {string, number} value - the value or the index to delete* @returns {void}*/instancevar.removeoption = function(value){switch(typeof(value)){case number:instancevar.parent().children(ol).children(:nth(+value+)).remove();break; case string:instancevar.parent().children(ol).children().each(function(index, optionvalue){if($(optionvalue).text()==value){$(optionvalue).remove();}});break;} };/*** resets the select to it's original* @returns {void}*/instancevar.restoreselect = function(){var originalselect = instancevar.parent().children(select);var objid = instancevar.attr(id);instancevar.parent().before(originalselect);instancevar.parent().remove();originalselect.css({visibility: visible, display: inline-block});originalselect.attr({id: objid});};//return the instancereturn instancevar;};/** private methods **///prepareoption函数的作用就是在当前下拉框中添加新选项function prepareoption(value, wrapper){var selectoption = $(+value+).appendto(wrapper.children(ol));var inputselect = wrapper.children(input);selectoption.css({padding: 3px,textalign: left,cursor: pointer }).hover(function(){selectoption.css({backgroundcolor: #eee});},function(){selectoption.css({backgroundcolor: #fff}); });//bind click on this optionselectoption.click(function(){inputselect.val(selectoption.text());inputselect.trigger(change);}); }}( jquery ));
其它类似信息

推荐信息