如何发布具有多个选定值的 HTML 列表框的选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/379432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post the selections of an HTML List Box with multiple selected values
提问by skb
I have a listbox on an HTML form with a Submit button. The listbox has multiple selection enabled. I am able to select multiple values in the listbox, but I don't know how to figure out what values were selected when the form is submitted. Also, I am adding user generated values to the list box dynamically using JavaScript, and I would like to be able to tell two things when the form submits:
我在带有提交按钮的 HTML 表单上有一个列表框。列表框启用了多项选择。我可以在列表框中选择多个值,但我不知道如何确定提交表单时选择了哪些值。此外,我正在使用 JavaScript 动态地将用户生成的值添加到列表框中,并且我希望能够在表单提交时说明两件事:
- What are the options added to the box by the user?
- What values are selected in the box by the user?
- 用户添加到框中的选项是什么?
- 用户在框中选择了哪些值?
Is this possible? Thanks.
这可能吗?谢谢。
采纳答案by some
Below you find an example of a page.
下面是一个页面示例。
Note that:
注意:
- the select element (and any form element) needs a name to be included in the post.
- only selected options in the select element will be posted.
- select 元素(和任何表单元素)需要在帖子中包含一个名称。
- 只会发布 select 元素中的选定选项。
What values are selected in the box by the user?
用户在框中选择了哪些值?
When the user submits the form only the selected values will be sent to the server. Depending on what language you are using at the server there are different ways to access them.
当用户提交表单时,只有选定的值会被发送到服务器。根据您在服务器上使用的语言,有不同的访问方式。
What are the options added to the box by the user?
用户添加到框中的选项是什么?
As stated before you only see what options that was selected. But how do you distinguish between your options and the users options? That depends on what data you are sending the user. The universal answer is thats the value you get back that you didn't send. However this can be simplified depending on your data. Since the options have both a value and a text property, one way it to use a numeric value for your pregenerated values. That way your values will be numeric in the reply and the users values will be a text string. This approach assumes that your user will not enter just a numeric value. Another approach is to add a prefix to all user generated values (remember you have both a value and a text field for all options)
如前所述,您只能看到选择了哪些选项。但是您如何区分您的选项和用户选项?这取决于您向用户发送的数据。普遍的答案是,这就是您没有发送的价值。但是,这可以根据您的数据进行简化。由于选项同时具有值和文本属性,因此可以使用数值作为预先生成的值的一种方法。这样你的值在回复中将是数字,用户值将是一个文本字符串。这种方法假设您的用户不会只输入数值。另一种方法是为所有用户生成的值添加前缀(请记住,所有选项都有一个值和一个文本字段)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test of select box</title>
<script type="text/javascript">
function addOpt(e) {
var o=document.createElement("option");
//o.value= -e.options.length;
o.text = "Test " + e.options.length;
o.selected=true;
e.add(o,null);
}
</script>
</head>
<body>
<form method="post" action="mypage.html">
<input type="button" onclick="addOpt(this.form.myselect)" value="Add option"/>
<br/>
<select id="myselect" name="mydata" multiple="multiple" size="10">
<option value="0">Test 0</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>
<br/>
<input type="submit"/>
</form>
</body>
</html>
回答by clawr
By naming your selectwith trailing square brackets, PHP (and likely other server languages) will put the data in an array
通过select用尾随方括号命名您,PHP(以及可能的其他服务器语言)会将数据放入数组中
ex:
前任:
<form action="process.php" method="post">
<select name="multiSelects[]" multiple="multiple" size="5">
<option value="0">Zero</option>
<option value="1">One</option>
</select>
<input type="submit" />
</form>
The selections will be available in the array $_POST['multiSelects']
选择将在数组中可用 $_POST['multiSelects']

