我为网页制作了一个组合框。它从用户那里获取值到文本框中,并在双击文本框时将这些值添加到列表中。我想让用户输入的值作为选项永久存储在列表中。我该怎么做。还有一个问题是如何计算列表中选项的数量,以便在其旁边添加一个元素。
这是我的代码。
<html>
<head>
<script language="javascript">
function AddListItem(form)
{
var TestVar = form.txtInput.value;
form.txtInput.value = "";
form.select.options[3]=new Option(TestVar, TestVar, true);
}
</script>
<head>
<body>
<form id='Form1'>
<input id='txtInput' type='text' maxlength = "5" size="5" ondblclick="AddListItem(this.form)"/>
<p>
<select id='select'>
<option>abc</option>
<option>cde</option>
<option>efg</option>
</select>
</form>
</body>
</html>
原文由 XCeptable 发布,翻译遵循 CC BY-SA 4.0 许可协议
要永久添加,您需要一个服务器端脚本。
要临时添加,您可以使用 javascript:
要计算选项的数量:
(仅供概念使用,未作为功能测试)