$service = CityService::getInstance();
$cities = $service->getAllCities();
foreach($cities as $city){
echo"<option value='".$city->CityID."'>".ucfirst($city->CityName)."</option>";
}
这里的代码我从城市获取列表,我想在下拉列表中选中粘性,我应该在哪里应用“选中”???
答案 0 :(得分:0)
假设您的select
代码有name="city"
。那么在您的$_POST["city"]
中将成为所选城市。
因此,当您遍历所有城市时,检查,$_POST["city"]
(由用户设置的内容)是否与循环中的当前项目相同:
foreach ($cities as $city) {
$selected = '';
if (!empty($_POST["city"]) && $_POST["city"] == $city->CityID) {
$selected = ' selected="selected"';
}
echo"<option value='" . $city->CityID . "'" . $selected . ">" . ucfirst($city->CityName) . "</option>";
}
答案 1 :(得分:0)
http://www.w3schools.com/tags/att_option_selected.asp
service = CityService::getInstance();
$cities = $service->getAllCities();
foreach($cities as $city){
echo"<option value='".$city->CityID."' selected>".ucfirst($city->CityName)."</option>";
}