duanhuang1699 2010-06-06 21:10
浏览 10
已采纳

从带有链接的选择菜单中自动选择选项

I am trying to use php (and if not php, javascript) to link to a page and include a "?type=foo" or "#foo" at the end of the link so that when the linked page loads it will automatically select a specific option, depending on the end of the link, from a dropdown menu in a form. I have tried to search for this but do not know what this action is actually called, but I've seen it done before. Does anyone know how to achieve this? Thanks very much.

  • 写回答

2条回答 默认 最新

  • dongsimang4036 2010-06-07 03:54
    关注

    When I have created these in the past, for ease of use, I have simply inserted the selected value in the top of the select object, rather than roll through the whole list and mark the selected one as such when it is encountered.

    Assuming the page is called like page.php?theSelectedList=Cougar:

    <select name="theSelectList">
      <?php
        if( isset( $_GET['theSelectList'] ) )
          echo '<option selected="selected">'.$_GET['theSelectList'].'</option>';
      ?>
      <option>Aardvark</option>
      <option>Baboon</option>
      <option>Cougar</option>
      <option>Dog</option>
      <option>Elephant</option>
    </select>
    

    In that case, the option for Cougar would be displayed twice - once at the top of the list, and selected, and again further down the list in the normal location. This may sound confusing, but, in the instances I have used it, it has been quite intuitive.

    It's nice and simple, but, I will mention the caveat, that the above would need a bit of re-jigging if the Option Labels and the Option Values are different (like it each option has a numeric value, but a text label.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试