我希望根据变量的值选择表单中的select值。下面是我尝试的代码,虽然我总是在select中得到值female,因为我认为select参数总是被应用。完成此任务的正确方法是什么?
<li><label for="sex">Sex:</label> <select name="sex"> <option value="male" selected="<?php if(strcmp($Sex, 'male') ==0)echo 'select'; ?>">Male</option> <option value="female" selected="<?php if(strcmp($Sex, 'female') ==0)echo 'select'; ?>">Female</option> </select></li>
答案 0 :(得分:1)
<select> <option value="male" <?php if($Sex == 'male')echo 'selected="selected"'; ?>>Male</option>
<option value="female" <?php if($Sex == 'female')echo 'selected="selected"'; ?> >Female</option>
</select>
Empty selected =“”与selected =“selected”相同