0

下午好,

使用 cakephp,如何在更新操作的选择框中选择一个值?

在视图中,我得到这样的变量,例如:

<?php $category = $itemEditar['Anuncio']['category']; ?>

我需要从选择框中选择一个选项:

<select id="select_category" name="enquiry">
                    <option value="" >selecione</option>
                    <option value="Category1">Categoria1</option>
                    <option value="Category2">Categoria2</option>
                    <option value="Category3">Categoria2</option>
                </select>

要进行更新操作,我需要标记保存在数据库中的类别,并且不知道如何执行此操作。

4

2 回答 2

2

您想检查 $category 对每个选项或选项,如果它们匹配设置 selected 属性。

<select id="select_category" name="enquiry">
    <option value="" >selecione</option>
    <option<?= $category == "Category1"?" selected = 'selected'":"" ?> value="Category1">Categoria1</option>
    <option<?= $category == "Category2"?" selected = 'selected'":"" ?> value="Category2">Categoria2</option>
    <option<?= $category == "Category3"?" selected = 'selected'":"" ?> value="Category3">Categoria2</option>
</select>
于 2013-06-19T19:43:35.867 回答
0

正确答案是:

<select id="select_category" name="enquiry" >
                    <option value="Category1" <?php echo $category == "Category1"?" selected = 'selected'":"" ?> >Categoria1</option>
                    <option value="Category2" <?php echo $category == "Category2"?" selected = 'selected'":"" ?> >Categoria2</option>
                    <option value="Category3" <?php echo $category == "Category3"?" selected = 'selected'":"" ?> >Categoria2</option>
                </select>
于 2013-06-19T21:01:15.523 回答