Select2不会创建名称属性 [英] Select2 does not create name attribute
本文介绍了Select2不会创建名称属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用select2 jQuery PlugIn我创建了一个标签框。因此我使用了以下html表单代码:
With the select2 jQuery PlugIn I created a tag box. Therefore I used the following html form code:
<form action="someAction" method="post">
<fieldset>
<select id="tags" multiple="" name="tags">
<option value="tag1">val1</option>
<option value="tag2">val2</option>
<option value="tag3">val3</option>
</select>
<button type="submit" class="btn">Submit</button>
</fieldset>
</form>
Select2从中生成以下字段:
Select2 generates the following field from it:
<input type="text" class="select2-input" autocomplete="off" id="s2id_autogen1" style="width: 10px;">
这里的问题是 name
属性不见了。如果没有名称
$ _ POST 变量中获取输入字段的数据/文本>属性?如何管理?
The problem is here that the name
attribute is missing. How can I get the data/text of the input field out of the $_POST
variable in PHP if there is no name
attribute? How can I manage this?
设置JS如下:
$(document).ready(function() {
$("#tags").select2({
placeholder: "Insert tag",
allowClear: true,
minimumInputLength: 2,
maximumSelectionSize: 1,
minimumWidth: 200
});
});
推荐答案
这是一个老问题,但当前select2 4.xx版本的正确答案是将name属性设置为数组
it's an old question, but correct answer for current select2 4.x.x version is to set name attribute as array
<select id="tags" multiple="" name="tags[]">
然后你就可以获得带有$ _POST的数组:
and then you can get array with $_POST like that:
$_POST['tags']
这篇关于Select2不会创建名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文