我可能错过了问题的重点,但是您是在问如何使 from 字段显示为被选中?简单修改这一行:
echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
具有:
echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
如果这是您要选择的特定选项...
你可能有这样的事情:
while($row = mysql_fetch_array($result))
{
if ($sectorActive != $row['idSector']) {
echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
}else{
echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
}
}
好的,因此,如果您希望选择字段具有默认值,则需要将其作为元素selected="selected"
中的参数值。<option>
根据您的示例,如下所示:
<select name="gender">
<option>Male</option>
<option>Female</option>
<option selected="selected">Other</option>
<option>Not Specified</option>
</select>
因此,如果您有一个查询拉记录,并且您想确保选择了正确的值。您需要有一个随每条记录而变化的变量 - 并且您需要在该 HTML 插入中安装一些逻辑,如下所示:(此示例将输出与上述示例相同的 HTML...)
<?
$usersGender = 'Other'; // This value should be set via the loop, and from a database value.
?>
<select name="gender">
<option <?=($usersGender=='Male')?'selected="selected"':''?>>Male</option>
<option <?=($usersGender=='Female')?'selected="selected"':''?>>Female</option>
<option <?=($usersGender=='Other')?'selected="selected"':''?>>Other</option>
<option <?=($usersGender=='Not Specified')?'selected="selected"':''?>>Not Specified</option>
</select>