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

/*-------------------------------------------------------------------------------------------
函数名称: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. C#微信公众号开发系列教程(接收事件推送与消息排重)

    微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这样一个场景:当用户关注微信账号时,获取当前用户信息,然后将信息写到数据库中.类似于pc端网站的注册.可 ...

  2. stuts2的上传(转载)

    Struts2本身并没提供上传的组件,我们可以通过调用上传框架来实现文件的上传. 一.配置上传解析器 首先要配置项目的框架,也就是倒导入"struts2- core-2.2.1.jar&qu ...

  3. 教你如何通过ICCID找回丢失的的iPhone

    22日晚买了FACETIME,在某宝上买的.价格不贵,可以查到偷手机的人注册FT的号码,还可以查询手机被刷机和被维修的日期(这个很关键) 27日手机被刷机,遂买了某宝查询ICCID的服务,找到一串IC ...

  4. Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template

    原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...

  5. vxworks一个超级奇怪的错误(parse error before `char')

    void tserver(int id) { if(debug){ useResource(,id);} char msgBuf[MAX_MSG_LEN]; if (msgQReceive(myMsg ...

  6. [TYVJ1827]『Citric II』一道防AK好题

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 第二届『Citric杯』NOIP提高组模拟赛第一题 描述 Lemon认为在第一届『Citric』杯模拟赛中出的 ...

  7. centos解决ping unknown host的问题

    当ping www.baidu.com 的时候如果出现 unknown host的提示 再ping一下IP, ping 8.8.8.8 如果此时能ping通,那么就是DNS服务器没有设置,不能解析域名 ...

  8. 洛谷P1118 数字三角形游戏

    洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直 ...

  9. HW4.42

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  10. CentOS设置在同一窗口打开文件夹

    默认情况下,CentOS双击文件夹会打开一个新窗口.这对于习惯Windows的用户会感觉非常别扭.其实,如果用鼠标中键双击文件夹,就不会打开新窗口了.当然,也可以按照如下设置: 1.  打开任意一个文 ...