从下拉菜单中显示特定选项的内容

时间:2018-08-14 13:57:57

标签: php drop-down-menu

我正在尝试创建一个下拉菜单。所选项目显示该特定选项内容的位置。我怎么做?

<select name="season" id="season">
    <option selected="selected">Kies Seizoen</option>
    <?php
    $Seasons  = WaterpoloAPICached::call("Seasons", "getSeasons");
    $SeasonId = "";
    foreach ($Seasons as $Season)
    { ?>
        <option value="<?php echo $SeasonId = $Season->Id ?>"><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></option>
        <?php
    } ?>
</select>
<?php
$test = (isset($_POST['season'])) ? $_POST['season'] : '';

?>
<h2>
    <strong>Seizoen <?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></strong>
</h2>

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我用一个对象数组替换了您的$ Seasons对象,因此语法是相同的。这是您要找的吗?

<script
  src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
  integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
  crossorigin="anonymous"></script>
<script>
  $(function(){
    $("select#season").change(function(){ //whenever someone changes the select list with id of 'season'
      //I set the span within a strong within an h2, to the currently selected option's text value.
    $("h2 strong span").text($(this).find("option:selected").text());
    })
  })
</script>
  <select name="season" id="season">
  <option selected="selected">Kies Seizoen</option>
  <?php
    $Seasons    =  array(
      (object)array('Id' => 1, 'DateFrom' => '2018-01-01', 'DateTo' => '2018-3-31'  ),
      (object)array('Id' => 2, 'DateFrom' => '2018-04-01', 'DateTo' => '2018-06-30' ),
      (object)array('Id' => 3, 'DateFrom' => '2018-07-01', 'DateTo' => '2018-09-30' ),
      (object)array('Id' => 4, 'DateFrom' => '2018-10-01', 'DateTo' => '2018-12-31' )
    );
    $SeasonId   = "";
    foreach($Seasons as $Season) {
  ?>
      <option value="<?php echo $SeasonId = $Season->Id ?>"><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></option>
  <?php
    }  //foreach($Seasons as $Season) {
  ?>
</select>
  <?php
  $test = (isset($_POST['season'])) ? $_POST['season'] : '';

  ?>
  <h2><strong>Seizoen <span><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></span></strong><h2>