在输入框中键入其他选项值后,将其他选项值添加到选择框中
Adding additional option values to the selectbox after they are typed in the input box
我需要你的帮助。
我在下面有以下代码,如果在输入框中键入一个值后仅将一个值添加到选择框中(因此,使其成为可编辑的选择框),那么如果在输入框中键入另一个值,则选择框的内容将更改为新输入的值。
我想修改下面的代码,以便允许将其他键入的值添加到选择框中。
<html>
<style>
#list {
left: expression(this.previousSibling.offsetLeft);
width: expression(this.previousSibling.offsetWidth);
clip: expression("rect(2px auto 20px " + (this.previousSibling.offsetWidth - 20) + "px)");
overflow: hidden;
position: absolute;
top: -1px;
font-size: 9pt;
font-family: Arial;
}
#wrapper {
border: 1px solid blue;
display: block;
position: relative;
width: 180px;
height: 20px;
}
#text1 {
border: 0;
height: 18px;
width: 180px;
position: relative;
font-size: 9pt;
font-family: Arial;
padding: 2px;
}
</style>
<script language="javascript">
function getval() {
var select = document.getElementById('list')
document.getElementById('text1').value = select.options[select.selectedIndex].text
}//end of function
var oNewOption = null;
function AddListItem(oInput) {
var oSelect = oInput.nextSibling;
if (oNewOption == null) {
oNewOption = document.createElement("OPTION");
oSelect.options.add(oNewOption);
}
oNewOption.text = oInput.value;
oNewOption.value = oInput.value;
}
</script>
<body>
<div id="wrapper">
<input id="text1" type="text" onkeyup="AddListItem(this)"><select id="list" onchange="getval()">
</select>
</div>
</body>
</html>
为了做到这一点,我不得不将onkeyup更改为onchange。因此,只有在输入元素失去焦点后才会添加该选项。
<html>
<style>
#list {
left: expression(this.previousSibling.offsetLeft);
width: expression(this.previousSibling.offsetWidth);
clip: expression("rect(2px auto 20px " + (this.previousSibling.offsetWidth - 20) + "px)");
overflow: hidden;
position: absolute;
top: -1px;
font-size: 9pt;
font-family: Arial;
}
#wrapper {
border: 1px solid blue;
display: block;
position: relative;
width: 180px;
height: 20px;
}
#text1 {
border: 0;
height: 18px;
width: 180px;
position: relative;
font-size: 9pt;
font-family: Arial;
padding: 2px;
}
</style>
<script language="javascript">
function getval() {
var select = document.getElementById('list')
document.getElementById('text1').value = select.options[select.selectedIndex].text
}//end of function
var oNewOption = null;
function AddListItem(oInput) {
var oSelect = oInput.nextSibling;
oNewOption = document.createElement("OPTION");
oSelect.options.add(oNewOption);
oNewOption.text = oInput.value;
oNewOption.value = oInput.value;
oNewOption.selected = true;
}
</script>
<body>
<div id="wrapper">
<input id="text1" type="text" onchange="AddListItem(this)"><select id="list" onchange="getval()">
</select>
</div>
</body>
</html>
相关文章:
- 如何使用CSS设置所选选项的样式,而不设置所有其他选项的样式
- 编写脚本以关闭其他选项卡或浏览器
- 从下拉列表中选择其他选项时激活文本框
- DateRangePicker 不适用于“今天”,但可以使用其他选项
- 下拉菜单的第一个选项不是一个选项;强制使用其他选项
- 删除在单击其他选项卡时生成的url查询元素的一部分
- 在这种javascript情况下,除了eval之外的任何其他选项
- 在其他选项卡中打开链接
- 在更改或其他选项上
- 工作流程/选项以扩展其他选项
- jQuery多选列表框,其中“无”选项取消选择所有其他选项
- 如何使用 JQuery 隐藏其他选项卡内容并仅显示选定的选项卡内容
- 选择带有“其他”选项的框未显示在Chrome和Firefox中
- 如何在单击 Webkit 通知时从浏览器中的任何其他选项卡移回当前选项卡
- 两个带有“其他”选项的选择框
- 获取选项 从其他选项中选择名称 选择不带表单标记的名称
- 是否可以使用 Javascript 库测试 SVG 元素?任何其他选项
- 当我悬停当前选项卡时,更改其他选项卡的不透明度
- 当我单击激活选项卡中的按钮时,如何使其他选项卡单击元素
- 在输入框中键入其他选项值后,将其他选项值添加到选择框中