0

我有一个用 php 生成的下拉菜单。

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
                    <? foreach($diplomas->result_array() as $diploma) {
          echo "<option    value=".$diploma['id'].">".$diploma['name']."</option>";
                            } ?>
</select>

我也有<? echo $edu['diploma']; ?>which 是数据库中的一个值。我想添加selected="selected"到下拉列表中与此值匹配的选项。我应该只使用jquery吗?最好的方法是什么?

4

5 回答 5

1

您可以像这样检查循环内部:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? 
    foreach($diplomas->result_array() as $diploma) 
        {
            if($edu['diploma']==$diploma['name'])
            {
                echo "<option selected ='selected' value=".$diploma['id'].">".$diploma['name']."</option>";
            }
            else
            {
                echo "<option value=".$diploma['id'].">".$diploma['name']."</option>";
            }
        } 
    ?>
</select>

或者使代码更短:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? 
    foreach($diplomas->result_array() as $diploma) 
        {
            $selected = ($edu['diploma']==$diploma['name']) ? " selected='selected' " : "" ;
            echo "<option $selected value=".$diploma['id'].">".$diploma['name']."</option>";
        } 
    ?>
</select>
于 2012-09-08T13:07:30.187 回答
1

使用相同的 PHP 代码:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <? foreach($diplomas->result_array() as $diploma) {
        echo "<option value=".$diploma['id'];
        if ($edu['diploma'] == $diploma['name']) {
            echo 'selected="selected"';
        }
        echo ">".$diploma['name']."</option>";
    } ?>
</select>

代码$diploma['name']$edu['diploma']每个选项的变量进行比较,selected="selected"如果它们相同则输出。

根据$edu['diploma']您需要将其与一件事或另一件事进行比较的内容。如果$edu['diploma']包含对的id更改。$diploma['name']$diploma['id']

如果$edu['diploma']包含文凭对象本身,则需要这样比较:$edu['diploma']['id'] == $diploma['id']

于 2012-09-08T13:09:11.190 回答
1

这就是我想出的:

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
    <?php foreach ($diplomas->result_array() as $diploma) {
    //echo "<option value='{$diploma['id']}'>{$diploma['name']}</option>";
    echo "<option value='{$diploma['id']}";
    if ($edu['diploma'] === $diploma['name']) {
        echo " selected";
    }
    echo ">{$diploma['name']}</option>\n";
} ?>
</select>
于 2012-09-08T13:09:37.240 回答
0
<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
   <?php foreach($diplomas->result_array() as $diploma) {
      $selected = $diploma['id'] == $edu['diploma'] ? ' selected="selected"' : ''; 
      echo '<option value="' . $diploma['id'] . '"' . $selected . '>' . $diploma['name'] . '</option>';
     } ?>
</select>

在循环内部进行检查,如果值匹配,则回显选定的属性。注意,我在测试中使用了三元语句

于 2012-09-08T13:09:48.757 回答
0

如果我没听错,试试这个;

<select name="diploma" id="diploma-<? echo $edu['id']; ?>">
<?
$selected = $edu['diploma'];
foreach($diplomas->result_array() as $diploma) {
  if($diploma['name'] == $selected){
    echo '<option selected="selected" value="'.$diploma['id'].'">'.$diploma['name']."</option>";
  }else{
    echo '<option value="'.$diploma['id'].'">'.$diploma['name']."</option>";
  }
}
?>
</select>
于 2012-09-08T13:21:11.953 回答