我可以动态创建多个列表框,但我无法发布值。 select元素是一个二维数组,我没有从选项中返回任何值。
这是我的javascript代码:
function createSelectCell(cell,column,multiselect_type_flag)
{
var copyListBox = document.getElementById(column);
var newListBox = document.createElement('select');
if(multiselect_type_flag == 'F')
{
newListBox.name = 'column_array_'+column+'[]';
}
else if(multiselect_type_flag == 'T')
{
newListBox.name = 'column_array_'+column+'[][]';
newListBox.size = '4';
newListBox.setAttribute('multiple', 'multiple');
}
for (var i = 0; i < copyListBox.options.length; i++)
{
var newOption = document.createElement("option");
newOption.text = copyListBox.options[i].text;
newOption.value = copyListBox.options[i].value;
newListBox.appendChild(newOption);
}
cell.appendChild(newListBox);
}
请求评论中的html:
echo "<td><select id='".$column."' name='column_array_".$column."[$i][]' multiple='multiple' size='4'>";
当我print_r($_POST['column_array_'.$column]);
时,我得到:
Array ( [0] => Array ( [0] => Secondary Contact ) [1] => Array ( [0] => Authorised to log calls ) )
当我应该得到:
Array ( [0] => Array ( [0] => Secondary Contact [1] => Authorised to log calls ) )
答案 0 :(得分:0)
我在代码中添加了以下内容:
var arrayIndex = rowCount - 1;
并为数组提供了一个索引:
newListBox.name = 'column_array_'+column+'['+arrayIndex +'][]';
答案 1 :(得分:0)
这样:
Array ( [0] => Array ( [0] => Secondary Contact ) [1] => Array ( [0] => Authorised to log calls ) )
因为你用[] []命名你的选择,而是使用单括号和适当的父索引,例如:
<select name="column_array['columnA'][]"> ...o2,o3... </select>
<select name="column_array['columnB'][]"> ...o1,o4... </select>
所以结果将是:
print_r($_POST['column_array'])
Array
(
[columnA] => Array
(
[0] => 2
[1] => 3
)
[columnB] => Array
(
[0] => 1
[1] => 4
)
)