繁体   English   中英

在更新中,如何从选择框中选择一个选项?

[英]in an update, how to select an option from a select box?

下午好,

使用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>

要进行更新操作,我需要标记数据库中保存的类别,并且我没有得到如何执行此操作。

您想要针对每个选项或选项检查$ category,如果匹配则设置所选属性。

<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>

正确答案是:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM