javascript实现可编辑的下拉框
曾经遇到过一个需求的情况是这样的,我们提供给用户的输入框的可选择项只能满足用户的大部分情况的选择,但是有时候会遇到一些用户想要输入的数据是下拉项中所没有的,而用户不希望改变下拉项为输入框模式,需要说如果实在无法满足时,允许用户进行输入。由此产生了一个既可以实现下拉选择,又可以输入的下拉框功能。以下是我实现的代码:
/*-------------------------------------------------------------------------------------------
函数名称: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实现可编辑的下拉框的更多相关文章
- 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”
接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...
- 自制jquery可编辑的下拉框
昨天看到QQ登录的时候,可以选择以前登录过的账户,这个东西也可以在网站登录的时候用到,所以我就想做一个这样的插件:在网上查了很多,没有找到合适自己的,所以决定自动制作一个. 原理就是一个textbox ...
- javascript 可多选的下拉框 multiselect
首先引用一个写的很好的博客http://www.cnblogs.com/landeanfen/p/5013452.html 我使用的是bootstrap-multiselect,实现功能是 选择下拉框 ...
- html 可编辑的下拉框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- javascript 可多选的下拉框 multiselect 动态删除option值,动态添加option值,动态生成表格
首先引用一个写的很好的博客http://www.cnblogs.com/landeanfen/p/5013452.html 我使用的是bootstrap-multiselect,实现功能是 选择下拉框 ...
- angularJS实现可编辑的下拉框
将angularJS与插件select2结合使用即可 <!DOCTYPE html><html lang="en"><head> < ...
- EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件
主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...
- jquery实现可编辑的下拉框( input + select )
HTML: <input id="inputModel" /> <select name="EngineModel" size="1 ...
- 下拉框的change事件
6.1,获取下拉框的值(html标签中没有onchange事件的) <script language="javascript"> $(document).ready(f ...
随机推荐
- php.ini 干了些啥?
今天又重新看了一遍php.ini 的各种配置介绍,感觉还是官网说的比较靠谱,朋友,你所要找的,都在这里了. http://www.php.net/manual/zh/ini.core.php
- App集成支付宝
转自:http://www.cnblogs.com/qianxudetianxia/archive/2012/04/04/2432406.html 手机的在线支付,被认为是2012年最看好的功能,我个 ...
- 普通Java类获取spring 容器的bean的5种方法
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...
- 自动测试框架(by myself)
这段日子以来一直在自动话测试,然后关于框架一直有个很模糊的概念,通过N多人的解说,这个应该不能算是一个框架,但是还是很模糊 如下图是我自己认为的框架,不知道是否正确(请大侠们多多指点) 1.用nuni ...
- 修改ruby gem源为ruby.taobao.org
由于网络原因,导致从rubygems.org下载gem文件较慢或者间歇性的连接失败,所以可以修改gem源为ruby.taobao.org.具体可以用 gem install rails -V 来查看执 ...
- C#索引器的作用及使用
1. 作用: 可以使得类和实例能够像数组那样使用一样,又称为带参属性 2. 区分 (1)索引器与数组的比较: 索引器的索引值不受类型限制.用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值 ...
- 【转】centos 6.4 samba 安装配置
关掉防火墙 service iptables stop chkconfig iptables off (永久关闭防火墙) 关闭SELinux vi /etc/sysconfig/selinu ...
- SVN记住用户名和密码后如何修改
今天遇到一个SVN检出代码用户验证问题.由于自己最近参与了好几个项目,一时间忙不过来.所以希望跟着自己的试用期的同事帮我测试一下刚修改完成的新功能是否有问题.但是该同事没有项目中权限,正好今天恰逢星期 ...
- 【待修改】nyoj 38 最小生成树
package nyoj; import java.util.Scanner; public class Main { public static void main(String args[]) { ...
- Java笔记(九)……面向对象I
面向对象概念 理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将功能封装进对象,强调具备了功能的对象. 面向对象是基于面向过程的. 面向 ...