twitter-bootstrap 由“bootstrap-select”中带有“multiple”的表单提交的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25267238/
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
Values submitted by a form with `multiple` in `bootstrap-select`
提问by Willem Van Onsem
If one generates a form with bootstrapand bootstrap-select:
如果生成带有bootstrap和的表单bootstrap-select:
<form action="handle.php" method="post">
<select name="pids" class="selectpicker" multiple>
<option value="1">A</option>
<option value="2">B</option>
<option value="6">C</option>
</select>
</form>
And one select all or multiple items, only the item with the last selected valueis posted to the handle.phppage.
并且选择所有或多个项目,只有最后选择的项目value才会发布到handle.php页面。
handle.php
handle.php
<?php
var_dump($_POST);
?>
result:
结果:
array(1) { ["pids"]=> string(1) "6" }
how can one retrieve all selected items?
如何检索所有选定的项目?
回答by Dave O'Dwyer
Its most likely a case that you need to set the name to pids[] (note the square brackets) The square brackets define an array instead of a single value.
最有可能的情况是您需要将名称设置为 pids[](注意方括号) 方括号定义了一个数组而不是单个值。
<select name="pids[]" class="selectpicker" multiple>
回答by Webice
<select name="pids[]" class="selectpicker" multiple>
Iam not sure but something like that :)
我不确定,但类似的事情:)
回答by Rbbn
Just a quick note...even though this one is accepted since two years...
只是一个简短的说明......即使这个已经被接受了两年......
For this to work you must POST to a file (which makes it reload). I tried figuring it out for 5 hours.
为此,您必须 POST 到一个文件(这会使其重新加载)。我试了 5 个小时才弄明白。
But, if you want to use Ajax...then you can send all values by
但是,如果您想使用 Ajax...那么您可以通过以下方式发送所有值
var values_from_list=$('#id_of_the_field').val();
and it will send all to the little ajax array, and in PHP you can json_encode it and save it to your db.
它会将所有内容发送到小 ajax 数组,在 PHP 中,您可以对其进行 json_encode 并将其保存到您的数据库中。
Hope it saves someone time.
希望它可以节省某人的时间。

