js實(shí)現(xiàn)select組件的選擇輸入過濾代碼
更新時(shí)間:2014年10月14日 17:20:51 投稿:whsnow
如何實(shí)現(xiàn)select組件的選擇輸入過濾作用,下面有一段js代碼,很實(shí)用,需要的朋友可以看看
實(shí)現(xiàn)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()指的就是對當(dāng)前對象的遍歷,這里的當(dāng)前對象指代的就是對當(dāng)前兩個(gè)下拉選擇框?qū)ο蟮囊灰槐闅v
this.each(function(){
var originalSelect = $(this);
//check if element is a select
if(originalSelect[0].tagName.toUpperCase()==="SELECT"){
//wrap the original select在原始的<select>外圍插入<div></div>框
originalSelect.wrap($("<div/>"));
var wrapper = originalSelect.parent();
wrapper.css({display: "inline-block"});
//place an input which will represent the editable select
//在選擇菜單上加入input輸入框<input alt title ..... style="width:......" value="">
var inputSelect = $("<input/>").insertBefore(originalSelect);
//get and remove the original id
var objID = originalSelect.attr("id");
originalSelect.removeAttr("id");
//add the attributes from the original select
//input輸入框的各種屬性設(shè)置
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 select
var 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 right
var triangle = $("<div/>").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
//聚焦的時(shí)候加上<ol><ol/>下拉框
var selectList = $("<ol/>").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變量中存儲當(dāng)前下拉框中的所有數(shù)據(jù)
//******
var resourceData = [];
originalSelect.children().each(function(index, value){
prepareOption($(value).text(), wrapper);
resourceData.push($(value).text());
});
//******
//bind the focus handler
//在鼠標(biāo)聚焦的時(shí)候fadeIn(100),即下拉顯示當(dāng)前下拉框
inputSelect.focus(function(){
selectList.fadeIn(100);
//v存儲的是在input輸入框中輸入的內(nèi)容,如果輸入的內(nèi)容不為空,就在存儲原始下拉框的所有數(shù)據(jù)中找到出現(xiàn)v中字符的數(shù)據(jù)壓入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快捷鍵按下后彈起的時(shí)候執(zhí)行的事件
//******
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 element
originalSelect.css({visibility: "hidden", display: "none"});
//save this instance to return it
instanceVar = inputSelect
}else{
//not a select
return 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 instance
return instanceVar;
};
/** private methods **/
//prepareOption函數(shù)的作用就是在當(dāng)前下拉框中添加新選項(xiàng)
function prepareOption(value, wrapper){
var selectOption = $("<li>"+value+"</li>").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 option
selectOption.click(function(){
inputSelect.val(selectOption.text());
inputSelect.trigger("change");
});
}
}( jQuery ));
您可能感興趣的文章:
- js實(shí)時(shí)獲取并顯示當(dāng)前時(shí)間的方法
- Ajax在線提交留言并實(shí)時(shí)顯示的js代碼[修正版]
- JS實(shí)現(xiàn)的5級聯(lián)動Select下拉選擇框?qū)嵗?/a>
- js給selected添加options的方法
- js實(shí)現(xiàn)Select下拉框具有輸入功能的方法
- js實(shí)現(xiàn)select跳轉(zhuǎn)功能代碼
- js添加select下默認(rèn)的option的value和text的方法
- js觸發(fā)select onchange事件的小技巧
- js 觸發(fā)select onchange事件代碼
- JS獲取select的value和text值的簡單實(shí)例
- js自動查找select下拉的菜單并選擇(示例代碼)
- js實(shí)現(xiàn)Select頭像選擇實(shí)時(shí)預(yù)覽代碼
相關(guān)文章
Bootstrap Tree View簡單而優(yōu)雅的樹結(jié)構(gòu)組件實(shí)例解析
本文通過實(shí)例代碼給大家介紹了Bootstrap Tree View簡單而優(yōu)雅的樹結(jié)構(gòu)組件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
《JavaScript高級程序設(shè)計(jì)》閱讀筆記(二) ECMAScript中的原始類型
ECMAScript有5種原始類型(primitive type),即Undefined、Null、Boolean、Number和String。ECMAScript提供了typeof來判斷值的類型2012-02-02
從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例
這篇文章主要為大家介紹了從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
js實(shí)現(xiàn)按鈕開關(guān)單機(jī)下拉菜單效果
這篇文章主要介紹了js實(shí)現(xiàn)按鈕開關(guān)單機(jī)下拉菜單效果,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
Java通過WebSocket實(shí)現(xiàn)異步導(dǎo)出解決思路
這篇文章主要介紹了通過WebSocket實(shí)現(xiàn)異步導(dǎo)出,本篇文章記錄大批量數(shù)據(jù)導(dǎo)出時(shí)間過長,導(dǎo)致接口請求超時(shí)問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01

