使用jQuery添加/删除未在POST中发送的选择列表-selectID


Add/Remove Select Lists using jQuery - selectID not being sent in POST

我使用的是这个网站的代码:使用jQuery 添加/删除选择列表

我已经设置好了显示部分,但当我提交表单时,$_POST中没有任何设置可以让我读取用户的选择。

这是我设置的表格:

<?php
    <form id='setTasks' action='?action=ae&view=departmentTasks' method='post'>
        <table>
            <tr>
                <td valign='top'>
                    <select name='selectfrom' id='select-from' multiple size='5'>";
                        for ($i = 0; $i < count($unusedTasks); $i++)
                            echo "<option value='" . $unusedTasks[$i]['taskID'] . "'>" . $unusedTasks[$i]['taskName'] . "</option>";
                echo "</select>
                </td>
                <td valign='top'><a href='JavaScript:void(0);' id='btn-add'>Add &raquo;</a></td>
                <td valign='bottom'><a href='JavaScript:void(0);' id='btn-remove'>&laquo; Remove</a></td>
                <td>
                    <select name='selectto' id='select-to' multiple size='5'>";
                        for ($i = 0; $i < count($departmentTasks); $i++)
                            echo "<option value='" . $departmentTasks[$i]['taskID'] . "'>" . $departmentTasks[$i]['taskName'] . "</option>";
            echo "</select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type='hidden' name='departmentID' value='" . $departmentID . "' />
                    <input type='hidden' name='submitFinal' value='1' />
                    <input type='submit' name='submitForm' value='Assign Tasks' />&nbsp;&nbsp;
                    <input type='button' value='Cancel' onclick='javascript:history.go(-1);' />
                </td>
            </tr>
        </table>
    </form>";
?>

和功能:

$(document).ready(function() {
    $('#btn-add').click(function(){
        $('#select-from option:selected').each( function() {
            $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
                $(this).remove();
            });
    });
    $('#btn-remove').click(function(){
        $('#select-to option:selected').each( function() {
            $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
            $(this).remove();
        });
        }); 
    });

前端(来回移动项目)运行良好,但当我执行var_dump($_POST)时,我不会从选择字段中获得任何信息。有人能看到我缺了什么吗?

您不会得到任何表单数据,因为选择列表只发送所选项。

一个解决方案:循环浏览所有选项,并在发布前将其设置为选中。

另一种解决方案是:在发布之前,将选项中所需的信息复制到隐藏字段中。