如何使 HTML 组合框保存用户在文本框中添加的值?
我为网页制作了一个组合框。它将用户的值放入文本框并显示。双击文本框中将它们添加到列表中。我想让用户输入的值永久存储为列表中的选项。我该怎么办呢。还有一个问题是如何计算列表中选项的数量,以便在其旁边添加一个元素。
这是我的代码。
<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>
I have made a combobox for a web page. It takes values from user into text box & adds those to list on double click in text box. I want to make user entered values permanently stored as option in list. How can I do it. One more question is how can I count the number of options in list so that I add an element next to that.
Here is my code.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要永久添加,您需要一个服务器端脚本。
要临时添加,您可以使用 javascript:
计算选项数量:(
仅用于概念用途,未作为功能进行测试)
To permanently add you need a server-side script.
To temporarily add you can use javascript:
To count the number of options:
(only for conceptual use, not tested as functional)
您可以像这样动态添加
:
selectId 是
对于您的代码,将其称为如
您所见,您实际上不需要找到选项的长度,但是您可以通过
options.length
来做到这一点 :说,
下拉菜单,以及传递给
服务器,添加到某个表,用于
实例。你可能必须这样做
当您将其添加到时,通过 AJAX 调用
下拉菜单
双击文本框是
不太好用。模糊可能是
选项。更好的是在后面添加一个“添加”按钮
文本框。
You add an
<option>
dynamically like this:selectId is the id of
<select>
,optText
is the text to be displayed in the dropdown andoptValue
is the value that will be sumbitted to the server.For your code, call it as
As you see, you don't really need to find the length of the options, but you can do it via
options.length
:That said,
dropdown, as well as to pass to the
server, to add to some table, for
instance. You might have to do that
call via AJAX, when you add it to
the dropdown
on double click of the textbox is
not very usable. On blur might be an
option. Better is an 'Add' button after the
textbox .
听起来您需要一个服务器端脚本。当您提交表单时,您可以有一个“记住”所有下拉选项的字段:
简化的 HTML:
简化的 PHP:
说明: PHP 将 ddOptions 保存在表单中 ->用户输入新选项->表格已提交-> PHP 查找存储的值 -> PHP推动新价值-> PHP 循环并创建永久下拉菜单。
Sounds like you need a server-side script then. When you submit the form, you can have a field that is 'remembering' all of the dropdown options:
The simplified HTML:
The simplified PHP:
To explain: PHP saves the ddOptions in the form -> User enters new option -> The form is submitted -> PHP finds the stored values -> PHP pushes on the new value -> PHP loops through and creates your permanent dropdown menu.