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