我正在尝试在一个页面和一个表单上处理多个具有相同名称的multi select
框/下拉列表。它们具有相同的名称,因为这些下拉/框是动态添加的。我的问题是如何单独检索每个选择框的数据。它显示了这样的结果:
Array(
[0] => M
[1] => T
[2] => W
)
但我想要像这样的结果
Array
(
[0] => Array(
[0] => M
[2] => T
)
[1] => Array(
[0] => W
)
)
我正在尝试这段代码:
<td class="v-align-middle semi-bold sorting_1">
<div class="form-group form-group-default form-group-default-select2 full-width days" id="111">
<label>SELECT DAYS</label>
<select id="dw" name="days[]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
<option value="M">Monday</option>
<option value="T">Tuesday</option>
<option value="W">Wednesday</option>
<option value="Th">Thursday</option>
<option value="F">Friday</option>
<option value="Sa">Saturday</option>
<option value="Su">Sunday</option>
</select>
</div> <br/>
</td>
<td>
这是我用来克隆我的表的代码:
$('.add-ins').on('click', function(){
var selfId = $(this).attr('id');
var tId = $(this).parent().parent().find('table').attr('id');
var lastId = $('.timings#'+tId+' tbody tr:visible:last').attr('id');
lastId++;
$('select').select2('destroy'); /* destroy select2 from select tag and then clone it */
var clonerow = $('.timings#'+tId+' tbody tr:visible:last').clone(true, true).attr('id', lastId);
clonerow.appendTo('.timings#'+tId+' tbody');
$('select').select2(); /* enable Select2 on the select elements */
});
PHP代码:
$days = $_POST['days'];
foreach($days as $value){
echo $value;
foreach($value as $week){
echo $week;
}
}
或者只是这个:
print_r($days);
请在这方面帮助我。如果有任何其他解决方案,比如为每个选择框使用不同的名称,那么请告诉我如何使用php $_POST
发布它们,因为我无法弄清楚用户将添加多少个多选框。
您的建议将受到高度赞赏。提前谢谢。
亲切的问候
答案 0 :(得分:3)
请在html中编码,如
<select id="dw" name="days[0][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
<option value="M">Monday</option>
<option value="T">Tuesday</option>
<option value="W">Wednesday</option>
<option value="Th">Thursday</option>
<option value="F">Friday</option>
<option value="Sa">Saturday</option>
<option value="Su">Sunday</option>
</select>
<select id="dw" name="days[1][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
<option value="M">Monday</option>
<option value="T">Tuesday</option>
<option value="W">Wednesday</option>
<option value="Th">Thursday</option>
<option value="F">Friday</option>
<option value="Sa">Saturday</option>
<option value="Su">Sunday</option>
</select>
答案 1 :(得分:1)
您应该使用以下方式:
1:
<select id="dw" name="days[0][]" >
<option value="M">Monday</option>
</select>
2:
<select id="dw" name="days[1][]" >
<option value="M">Monday</option>
</select>
如果您使用javascript
插入选择框,请使用变量为0/1/2...
选择设置name
。
POST数据:
Array
(
[0] => Array(
[0] => M
[1] => T
)
[1] => Array(
[0] => W
)
)