Select2不会创建名称属性
使用select2 jQuery PlugIn,我创建了一个标签框。 因此我使用了以下html表单代码:
Select2从中生成以下字段:
问题在于缺少name
属性。 如果没有name
属性,如何从PHP中的$_POST
变量中获取输入字段的数据/文本? 我该怎么办呢?
设置JS如下:
$(document).ready(function() { $("#tags").select2({ placeholder: "Insert tag", allowClear: true, minimumInputLength: 2, maximumSelectionSize: 1, minimumWidth: 200 });
});
我快速阅读了select2 Github帐户中的一些票据,其中一些人提到了这个问题。 看来select2只是没有添加name属性。
您可以尝试通过JS在表单提交上动态添加一个,看看是否有效,
$('form').submit(function() { $('#s2id_autogen1').attr('name', 'whatever'); return true; });
或者您可以通过ajax提交表单并通过ID获取值并以此方式发布
POST需要名称才能工作,但是我注意到它有一个id:
id="s2id_autogen1"
使用JS通过id获取值并发布它。
如果id不断变化,您可以使用该类:
HTML:
JS
$(document).ready(function(){ $("#tags").select2({ placeholder: "Insert tag", allowClear: true, minimumInputLength: 2, maximumSelectionSize: 1, minimumWidth: 200 }); //Make it so everytime the input with the class select2-input value is changed //the value gets copied into the element with the id myField $('input.select2-input').live('change', function(){ $('input#myField').val($(this).val()); });
});
像这样提交表单将自动包含POST变量中的值。
经过多次阅读,我决定更改select2.js本身。
在2109行将其更改为
this.focusser.attr("id", "s2id_"+this.select.context.id);
如果您的输入标记是这样的
因此,在列表中搜索的输入标记的id为s2id_fichier_search
据我所知,不应该有冲突,这将允许你在同一页面上有多个select2并通过他们的事件运行你的function,例如。
$(function() { $('#s2id_fichier_search').keyup(function() { console.log('Be Practical') }) }
所以这将像你要使用一样运行
我肯定用这个简单的片段解决了这个问题。
我添加了一个隐藏的输入:
这是一个老问题,但当前select2 4.xx版本的正确答案是将name属性设置为数组
然后你可以使用$ _POST获取数组:
$_POST['tags']