jQuery select2 多值仅在 POST 中发送一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19458499/
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
select2 multi-value only sending one value in POST
提问by Nate Atkinson
Here is a snippet of my code: http://jsfiddle.net/natatkinson/xbWEb/
这是我的代码片段:http: //jsfiddle.net/natatkinson/xbWEb/
Javascript:
Javascript:
$(document).ready(function () {
$("#tags").select2({
maximumSelectionSize: 3
});
});
HTML:
HTML:
<form name="form1" id="form1" action="" method="GET">
<fieldset>
<legend>Shoe Info</legend>
<dl> <dt>Name:</dt>
<dd>
<input type="text" name="name" id="name" size="40" placeholder="Shoe name" required autofocus>
</dd>
</dl> <dt>Tags:</dt>
<dd>
<select multiple="" name="tags" id="tags" style="width:100%;">
<option value="0">Select Tags</option>
<option value="2">racing flat</option>
<option value="3">track spikes</option>
<option value="1">trainer</option>
</select>
</dd>
</dl>
</fieldset>
<p class="submit-buttons">
<input type="submit" class="button1" name="submit" id="submit" value="Add Shoes" />
</p>
All that submits is 1 value, not a comma separated list like the documentation says.
提交的所有内容都是 1 个值,而不是像文档所说的逗号分隔列表。
回答by Ahmet Erkan ?EL?K
You must use [] for multiple value contained form control's name. Correct using at bottom:
对于包含多个值的表单控件名称,您必须使用 []。在底部正确使用:
<select multiple="" name="tags[]" id="tags" style="width:100%;">
回答by Frank
I think you can not get string data from select2 directly, you should add a hidden input, and submit the hidden input's value as param.
我认为您不能直接从 select2 获取字符串数据,您应该添加一个隐藏输入,并将隐藏输入的值作为参数提交。
<input type="hidden" name="tags" id="tags"></input>
<select multiple="" style="width:100%;" onchange="changeSelect(event)">
<option value="0">Select Tags</option>
<option value="1">trainer</option>
</select>
function changeSelect(event){
var select2Value = $(event.target).val();
$("tags").val(select2Value);
}