这是我写的代码,请帮我解决这个问题。
<select name="gender" id="gender">
<option value="M"<?php if(isset($_POST['gender'])=="M"){echo "selected='selected'";}?>>Male</option>
<option value="F"<?php if(isset($_POST['gender'])=="F"){echo "selected='selected'";}?>>Female</option>
<option value="O"<?php if(isset($_POST['gender'])=="O"){echo "selected='selected'";}?>>Other</option>
</select>
当我通过在下拉列表中选择男性来提交表单时,它应保留所选值,直到我在下一次提交表单时更改该值。
在上面的代码中,如果我选择男性并提交表格,则显示给我其他人。
所以请稍微帮忙。 在此先感谢那些想要解决问题的人。
答案 0 :(得分:6)
如果您使用以下内容将会更快更好: -
<select name="gender">
<option value="Male" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'Male')?'selected="selected"':''; ?> >Male</option>
<option value="Female" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'Female')?'selected="selected"':''; ?> >Female</option>
<option value="Other" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'other')?'selected="selected"':''; ?> >Other</option>
</select>
答案 1 :(得分:5)
假设你有这个:
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
然后在您提交的页面中,放置:
<select name="gender">
<option value="Male" <?php if ($_POST['gender'] == 'Male') echo 'selected="selected"'; ?> >Male</option>
<option value="Female" <?php if ($_POST['gender'] == 'Female') echo 'selected="selected"'; ?> >Female</option>
<option value="Other" <?php if ($_POST['gender'] == 'Other') echo 'selected="selected"'; ?> >Other</option>
</select>
答案 2 :(得分:0)
而不是
<option value="M"<?php
if(isset($_POST['gender'])=="M")
--------^^^^^^^^^^^^
{echo "selected='selected'";}?>>Male</option>
试
<option value="M"<?php if((!empty($_POST['gender']) && $_POST['gender']=="M")
------^^^^^^^^
{echo "selected='selected'";}?>>Male</option>