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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?