2015-10-07 35 views
0

我有一個下拉菜單,效果很好,頁面部分是否完美無瑕。問題在於選擇的頁面加載並顯示時,我需要它在下拉菜單中顯示選擇的名稱,因爲它是活動頁面。我有以下代碼:如何獲取選定的值以顯示DropDown菜單

 <div class="about-navDropWrapper"> 
      <select onchange="location = this.options[this.selectedIndex].value;"> 
       <?php 
          if (get_field('games_nav_drop_down_link', 'option')): 
           while(has_sub_field('games_nav_drop_down_link', 'option')) : 
         ?> 
         <option value="<?php echo get_sub_field('games_nav_drop_down_link_one', 'option'); ?>"><?php echo get_sub_field('games_nav_drop_down_name', 'option'); ?></option> 
         <?php 
          endwhile; 
          endif; 
         ?> 
      </select> 
     </div> 

有什麼建議嗎?

回答

1

您將爲選定頁面的選項元素添加selected="selected"屬性。你可以這樣做jQuery中被找到<option>你想要的元素的值(注我假設>在你values結束是一個錯字,並刪除它):

因此,例如,假設我們想要顯示page3.html

var selected = "page3.html"; // Would be replaced with the page by location 
 

 
$("#selectItem option[value='"+selected+"']").attr("selected", "selected");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="about-navDropWrapper"> 
 
      <select id="selectItem"> 
 
       <option value="page1.html">Page 1</option> 
 
       <option value="page2.html">Page 2</option> 
 
       <option value="page3.html">Page 3</option> 
 
       <option value="page4.html">Page 4</option> 
 
      </select> 
 
     </div>

+0

仍然不會爲我工作 – MikeL5799

+0

@ MikeL5799你能解釋一下爲什麼?這個例子工作正常。你確定你正在獲得'選擇'? –

+0

我的歉意Spencer我忽略提及這是WordPress的。我更新了代碼以顯示它的外觀 – MikeL5799

0

請試試這個:

<script> 
     onload = function() { 

      var thisPage = location.pathname.replace("/", ""); 

      document.getElementsByTagName("select")[0].value = thisPage; 
     } 
    </script> 
相關問題