曾经遇到过一个需求的情况是这样的,我们提供给用户的输入框的可选择项只能满足用户的大部分情况的选择,但是有时候会遇到一些用户想要输入的数据是下拉项中所没有的,而用户不希望改变下拉项为输入框模式,需要说如果实在无法满足时,允许用户进行输入。由此产生了一个既可以实现下拉选择,又可以输入的下拉框功能。以下是我实现的代码:

/*-------------------------------------------------------------------------------------------
函数名称:jsLab_CreateSelectInput()
函数功能:创建支持下拉输入框对象
函数参数:
selectId 下拉对象id
inputTextName 输入框name属性
inputTextInitValue 输入框默认值
函数输出:
生成支持下拉的输入框,既可以输入又可以下拉选择
-------------------------------------------------------------------------------------------*/
function jsLab_CreateSelectInput(selectId, inputTextName, inputTextInitValue)
{
/*判断传入的下拉参数是下拉对象还是下拉框id*/
var selectDom = typeof(selectId) == "object" ? selectId : document.getElementById(selectId); /*若下拉框没有name属性则给下拉框创建name属性*/
if(!selectDom.name)
{
selectDom.name = selectDom.id;
} if(typeof(inputTextName) == "undefined")
{
inputTextName = selectDom.name + "InputText";
} if(typeof(selectDom.parentNode) == "undefined")
{
return false;
}
/*下拉框所在的父节点以及父节点的定位位置*/
var selectParent = selectDom.parentNode;
// store the raw position of selectDom's parent
var selectParentRawWidth = selectParent.offsetWidth;
var selectParentRawHeight = selectParent.offsetHeight; // clip the selectDom
if(selectDom.style.position != "absolute")
{
//selectDom.style.position = "absolute";
}
// store the raw position of selectDom
var selectRawTop = selectDom.offsetTop;
var selectRawLeft = selectDom.offsetLeft; var selectOffsetWidth = selectDom.offsetWidth;
var selectOffsetHeight = selectDom.offsetHeight; /*设置下拉框可视部分*/
selectDom.style.clip = "rect(0px, "+ selectOffsetWidth +"px, "+ selectOffsetHeight +"px, "+(selectOffsetWidth - 4)+"px )"; /*判断下拉框是否存在inputText属性*/
if(typeof(selectDom.inputText) == "undefined")
{
var oInputText = document.createElement("input"); /*define the oInputText's style value for different browsers, 定义在不同浏览器下输入框的渲染样式*/
var inputTextHeight = selectOffsetHeight - 1;
var inputTextBorderWidth = 2;
var inputTextBorderStyle = "inset";
var inputTextBorderColor = "threedlightshadow";
var inputTextBorderBottomColor = inputTextBorderColor;
var inputTextFontSize = selectOffsetHeight - 7; if(navigator && navigator.userAgent.toLowerCase())
{
var navigatorInfo = navigator.userAgent.toLowerCase(); if(navigatorInfo.indexOf("opera") >= 0)
{
inputTextHeight += 1;
inputTextBorderWidth = 1;
inputTextBorderStyle = "ridge";
inputTextBorderColor = "threedlightshadow";
inputTextFontSize += 1;
}
else if(navigatorInfo.indexOf("firefox") >= 0)
{
inputTextHeight -= 2;
inputTextBorderBottomColor = "threeddarkshadow";
}
else if(navigatorInfo.indexOf("ie") >= 0)
{
inputTextHeight -= 2;
inputTextBorderBottomColor = "threeddarkshadow";
}
else
{
inputTextFontSize = inputTextFontSize - 1;
}
} // reset the position of select_parent
//selectParent.style.width = (selectParentRawWidth - 4)+"px";
//selectParent.style.height = (selectParentRawHeight)+"px"; // define the clip input's style,定义输入框的可视样式
oInputText.style.display = "block";
oInputText.style.position = "absolute";
oInputText.style.width = (selectOffsetWidth - 20) + "px";
oInputText.style.height = inputTextHeight + "px";
//oInputText.style.top = selectRawTop + "px";
//oInputText.style.left = selectRawLeft + "px";
oInputText.style.fontSize = inputTextFontSize + "px";
oInputText.style.paddingLeft = "2px";
oInputText.style.borderWidth = inputTextBorderWidth + "px";
oInputText.style.borderStyle = inputTextBorderStyle;
oInputText.style.borderColor = inputTextBorderColor;
oInputText.style.borderBottomWidth = "1px";
oInputText.style.borderBottomColor = inputTextBorderBottomColor;
oInputText.style.borderRightWidth = "0";
oInputText.name = inputTextName;
oInputText.title = "支持手工输入方式,也可以下拉选择方式!"; selectDom.inputText = oInputText; }
else
{
var oInputText = selectDom.inputText;
} /*将输入框对象插入到下拉框前面*/
selectParent.insertBefore(oInputText,selectDom); /*将下拉框中选中的值初始化到输入框中*/
jsLab_SetSelectInputValue(selectDom, inputTextInitValue); /*给下拉绑定监听事件, 当下拉变化时,将下拉框的值初始化到输入框中*/
if(selectDom.attachEvent)
{
/*支持非火狐浏览器*/
selectDom.attachEvent("onchange",function(){
jsLab_BuildSelectInput(selectDom,selectOffsetWidth,selectOffsetHeight);
});
}
else
{
/*仅支持火狐浏览器*/
selectDom.addEventListener("change",function(){
jsLab_BuildSelectInput(selectDom,selectOffsetWidth,selectOffsetHeight);
},false);
}
} /*-------------------------------------------------------------------------------------------
函数名称:jsLab_BuildSelectInput()
函数功能:根据下拉框的选择变化将下拉框的值赋到输入框中
函数参数:
selectDom : 下拉对象id
函数输出:
-------------------------------------------------------------------------------------------*/
function jsLab_BuildSelectInput(selectDom,selectOffsetWidth,selectOffsetHeight)
{
var selectedOption = selectDom.options[selectDom.selectedIndex];
var oInputText = selectDom.inputText; if(typeof(selectedOption.innerText) == "undefined")
{
oInputText.value = selectedOption.text;
}
else
{
oInputText.value = selectedOption.innerText;
} oInputText.style.display = 'inline';
oInputText.style.color = "highlighttext";
oInputText.style.backgroundColor = "highlight";
selectDom.style.clip = "rect(0px, "+ selectOffsetWidth +"px, "+ selectOffsetHeight +"px, "+(selectOffsetWidth - 19)+"px )"; /*给输入框绑定click事件*/
oInputText.onclick = function()
{
oInputText.style.backgroundColor = "";
oInputText.style.color = "";
};
/*给下拉框绑定与输入相同事件*/
selectDom.onblur = oInputText.onclick;
} /*-------------------------------------------------------------------------------------------
函数名称:jsLab_SetSelectInputValue()
函数功能:将下拉框中选中的值初始化到新创建的输入框中
函数参数:
selectDom : 下拉对象id
inputTextInitValue:输入框默认值
函数输出:
-------------------------------------------------------------------------------------------*/
function jsLab_SetSelectInputValue(selectDom, inputTextInitValue)
{
var selectedOption = selectDom.options[selectDom.selectedIndex]; if(typeof(selectedOption.innerText) == "undefined")
{
selectDom.inputText.value = inputTextInitValue.length > 0 ? inputTextInitValue : selectedOption.text;
}
else
{
selectDom.inputText.value = inputTextInitValue.length > 0 ? inputTextInitValue : selectedOption.innerText;
}
}

javascript实现可编辑的下拉框的更多相关文章

  1. 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”

    接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...

  2. 自制jquery可编辑的下拉框

    昨天看到QQ登录的时候,可以选择以前登录过的账户,这个东西也可以在网站登录的时候用到,所以我就想做一个这样的插件:在网上查了很多,没有找到合适自己的,所以决定自动制作一个. 原理就是一个textbox ...

  3. javascript 可多选的下拉框 multiselect

    首先引用一个写的很好的博客http://www.cnblogs.com/landeanfen/p/5013452.html 我使用的是bootstrap-multiselect,实现功能是 选择下拉框 ...

  4. html 可编辑的下拉框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. javascript 可多选的下拉框 multiselect 动态删除option值,动态添加option值,动态生成表格

    首先引用一个写的很好的博客http://www.cnblogs.com/landeanfen/p/5013452.html 我使用的是bootstrap-multiselect,实现功能是 选择下拉框 ...

  6. angularJS实现可编辑的下拉框

    将angularJS与插件select2结合使用即可 <!DOCTYPE html><html lang="en"><head>    < ...

  7. EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件

    主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...

  8. jquery实现可编辑的下拉框( input + select )

    HTML: <input id="inputModel" /> <select name="EngineModel" size="1 ...

  9. 下拉框的change事件

    6.1,获取下拉框的值(html标签中没有onchange事件的) <script language="javascript"> $(document).ready(f ...

随机推荐

  1. UITableView添加静态背景.

    1:  controller self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@" ...

  2. apache配置虚拟主机的三种方式

    Apache 配置虚拟主机三种方式   一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhos ...

  3. 利用URLRewriter重写url地址

    首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDN ...

  4. FZU 2237 中位数 主席树 树上k大

    #include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...

  5. NOIP2011普及组 瑞士环 保序性

    题目链接:http://noi.openjudge.cn/ch0401/4363/ 分析:如果直接模拟,时间复杂度是O(r*nlogn)超时 然后我们发现每次一轮开始时,刚开始是保序的,然后我们可以把 ...

  6. [King.yue]关于代码调试时的缓存问题的一个解决办法

    后台构造数据更改之后,页面数据一直不变,试过各种办法,清空浏览器缓存,停止端口重新运行,关掉程序重新打开,都不可以.最终全部重新生成之后,终于可以正常调试了. 另:调试ASP.NET网站程序的时候,为 ...

  7. win7下的vxworks总结

    在visualbox下运行vxworks 先来看一张效果图: 在tornado端 成功运行第一个程序,输出了visualbox can run the vxworks ! 在vmware下的速度快多了 ...

  8. chm 字体修改

    今天打开从网络下载的“[MSDN]Csharp编程指南+参考手册.chm”文件,以为看看里面所提供的一些知识点,但是发现文件显示的字体觉得有点别扭,以为能够像网页那样ctrl键+鼠标滚轮就能进行字体的 ...

  9. 【转】傅里叶变换 拉普拉斯变 z变换 DFT DCT意义

    傅里叶变换在物理学.数论.组合数学.信号处理.概率论.统计学.密码学.声学.光学.海洋学.结构动力学等领域都有着广泛的应用(例如在信号处理中,傅里叶变换的典型用途是将信号分解成幅值分量和频率分量). ...

  10. 【JS】Beginner1:Making Stuff Happen

    1.JS(JavaScript) is for interactivity 2.How does JS relate to HTML&CSS? script tag script elemen ...