douyanlu7380 2013-05-21 06:33
浏览 22
已采纳

选项显示为空白然后选择提交保持值

Is it possible to have the first item in a dropdown list blank and selected by default but when choosing an item in the dropdown list and hitting submit, having that value stay there.

here is an example

<form action="" method="post" />
<SELECT NAME="whatever" id="whatever">
<?php $selected = isset($_POST["whatever"]) && $_POST["whatever"] == 'whatever'; ?>
<option value="" hidden="true" selected="selected"></option>
<OPTION VALUE=1 onclick"selected='selected'">All</OPTION>
</SELECT> 
<input type="submit" name="submit" value="Update"/>
</form>

currently if I hit submit it will change the selected value back to blank.

  • 写回答

2条回答 默认 最新

  • douxian1892 2013-05-21 09:14
    关注

    u may use like this

    <form action="" method="post" />
    <SELECT NAME="whatever" id="whatever">
    <option value="">Select value</option>
    <OPTION VALUE=1 <?php if($_POST["whatever"]==1) echo "selected='selected'";?> >All</OPTION>
    <OPTION VALUE=2 <?php if($_POST["whatever"]==2) echo "selected='selected'";?> >Value One</OPTION>
    </SELECT> 
    <input type="submit" name="submit" value="Update"/>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?