用php隐藏组合框的选项是不可能的吗?

时间:2016-06-17 16:56:20

标签: php html combobox

在php中使用开关,我想根据从数据库中预先选择的值隐藏组合框的选项。我已经被告知要通过php switch语句来做这件事,这就是我得到的:

<tr>
                <td>
                <select name="cbostatus" id="cbostatus">
                <option selected "" ></option>
                <option value="1" <?php if ($row['task_status']=='Planned') echo 'selected="selected"';?>>Planned</option>
                <option value="2" <?php if ($row['task_status']=='Started') echo 'selected="selected"';?>>Started</option>
                <option value="3" <?php if ($row['task_status']=='Available') echo 'selected="selected"';?>>Available</option>
                <option value="4" <?php if ($row['task_status']=='Impeded') echo 'selected="selected"';?>>Impeded</option>
                <option value="5" <?php if ($row['task_status']=='Concluded') echo 'selected="selected"';?>>Finished</option>
                </select>
                </td>
                </tr>

这就是组合,这就是开关:

                <?php
                switch ($row['task_status'])
                {               
                    case 'Planned':
                        echo '
                            <script>
                                    cbostatus.options[4].style.display = "none"; 
                                    cbostatus.options[5].style.display = "none"; 
                                    cbostatus.options[3].style.display = "none"; 
                            </script>
                        ';
                        break;

                    case 'Available':
                        echo '
                            <script>
                                    cbostatus.options[1].style.display = "none"; 
                                    cbostatus.options[4].style.display = "none"; 
                                    cbostatus.options[5].style.display = "none"; 
                            </script>
                        ';
                        break;

                    case 'Started':
                        echo '
                            <script>
                                    cbostatus.options[1].style.display = "none"; 
                            </script>
                        ';
                        break;

                    case 'Impeded':
                        echo '
                            <script>
                                    cbostatus.options[1].style.display = "none"; 
                            </script>
                        ';
                        break;  

                    case 'Concluded':
                        echo '
                            <script>
                                    cbostatus.options[1].style.display = "none"; 
                            </script>
                        ';
                        break;  

                }
                ?>

但这并不奏效。为什么?这不可能吗?我现在已经尝试这样做三天了。有人有任何想法吗?

1 个答案:

答案 0 :(得分:2)

我不认为切换是最好的方法,我会放弃JavaScript并使用PHP完成所有操作。通过这种方式,您不必担心隐藏选项,因为只有您需要的选项才能开始。我的建议是:

为每种状态定义数值和可见选项的数组:

$status = [
    'Planned'   => ['val' => 1, 'options' => ['Planned','Started']],
    'Started'   => ['val' => 2, 'options' => ['Started','Available','Impeded','Concluded']],
    'Available' => ['val' => 3, 'options' => ['Started','Available']],
    'Impeded'   => ['val' => 4, 'options' => ['Started','Available','Impeded','Concluded']],
    'Concluded' => ['val' => 5, 'options' => ['Started','Available','Impeded','Concluded']],
];

然后根据任务状态输出您的选择选项,如下所示:

<tr>
    <td>
        <select name="cbostatus" id="cbostatus">
        <option selected "" ></option>
        <?php  foreach ($status[$row['task_status']]['options'] as $option) {
            $selected = $row['task_status'] == $option ? 'selected="selected"' : '';
            echo '<option value="'. $status[$option]['val'] .'">'. $option .'</option>';
        } ?>
        </select>
    </td>
</tr>