dongquelu1239 2019-04-05 05:54
浏览 126

更改页面加载时的选择选项值

I've got a dropdown with a list of regions, I'd like the drop down to auto select an item based on the variable value I assign.

Here's the code.

<select name="_sft_location[]" class="sf-input-select" title="">
    <option class="sf-level-0 sf-item-0" data-sf-count="0" data-sf-depth="0" value="">All Locations</option>
    <option class="sf-level-0 sf-item-177" data-sf-count="2" data-sf-depth="0" value="all">Location A</option>
    <option class="sf-level-0 sf-item-139" data-sf-count="24" data-sf-depth="0" value="vic">Location B</option>
</select>        

So for instance, if I have a PHP variable $defaultLocation = "Location B", the dropdown should autoselect "Location B" from the list on page load.

Hope this is clear.

  • 写回答

6条回答 默认 最新

  • duankuiyu4618 2019-04-05 05:57
    关注

    You need to use selected attribute of <option>

    So, if Location A and Location B needs to be selected, your code would be:

    If your options are getting generated run time from PHP, you need to add if condition there.

    <select name="_sft_location[]" class="sf-input-select" title="">
        <option class="sf-level-0 sf-item-0" data-sf-count="0" data-sf-depth="0" value="">All Locations</option>
        <option class="sf-level-0 sf-item-177" data-sf-count="2" data-sf-depth="0" value="all" selected="selected">Location A</option>
        <option class="sf-level-0 sf-item-139" data-sf-count="24" data-sf-depth="0" value="vic" selected="selected">Location B</option>
    </select>   
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号