简体   繁体   English

如何从下拉列表中继续显示所选选项?

[英]How to keep showing selected option from drop down list?

I have a drop down list where I select options 我有一个下拉列表,我在其中选择选项

<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

You can see the list here footystat 你可以在这里看到footystat列表

I am using the following PHP 我使用以下PHP

if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; } 

if(isset($_POST['year'])){ $yearette = $_POST['year']; }

if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }

if(isset($_POST['which'])){ $whichette = $_POST['which']; }

When I select something from the list, I want selected item in the list to continue showing. 当我从列表中选择某些内容时,我希望列表中的所选项目继续显示。 At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014. 在我选择(例如)2013/2014时,它将显示结果,但下拉菜单将返回其原始状态,而不是显示2013/2014。

Get Option value selected when it gets posted value, like this, 在获得发布值时选择选项值,如下所示,

<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>

Set value like this for each option 为每个选项设置这样的值

You can set the "selected" property to the option , just like you set a value ! 您可以将“selected”属性设置为该选项,就像设置值一样!

<option value="8" selected>2009/2010</option>

Use a if statement in PHP to determine which one should be selected. 在PHP中使用if语句来确定应该选择哪一个。

Thats because the page refreshes. 那是因为页面刷新了。

On page load check if there is post variable than match the value with each option's HTML and write selected attribute. 在页面加载时检查是否存在post变量,而不是将值与每个选项的HTML匹配并写入所选属性。

更短的方法是

<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM