2017-07-14 29 views
0

我有5種形式,我希望從單個頁面上的下拉框中進行選擇。我需要它以與製表符相同的方式工作,除了下拉框。有人請指出我的方向來完成這項工作嗎?使下拉工作像標籤

我想在下拉框中點擊以顯示tabcontent ...但沒有標籤按鈕。

-Chad

function openCity(evt, cityName) { 
 
    // Declare all variables 
 
    var i, tabcontent, tablinks; 
 

 
    // Get all elements with class="tabcontent" and hide them 
 
    tabcontent = document.getElementsByClassName("tabcontent"); 
 
    for (i = 0; i < tabcontent.length; i++) { 
 
     tabcontent[i].style.display = "none"; 
 
    } 
 

 
    // Get all elements with class="tablinks" and remove the class "active" 
 
    tablinks = document.getElementsByClassName("tablinks"); 
 
    for (i = 0; i < tablinks.length; i++) { 
 
     tablinks[i].className = tablinks[i].className.replace(" active", ""); 
 
    } 
 

 
    // Show the current tab, and add an "active" class to the button that opened the tab 
 
    document.getElementById(cityName).style.display = "block"; 
 
    evt.currentTarget.className += " active"; 
 
}
/* Style the tab */ 
 
div.tab { 
 
    overflow: hidden; 
 
    border: 1px solid #ccc; 
 
    background-color: #f1f1f1; 
 
} 
 

 
/* Style the buttons inside the tab */ 
 
div.tab button { 
 
    background-color: inherit; 
 
    float: left; 
 
    border: none; 
 
    outline: none; 
 
    cursor: pointer; 
 
    padding: 14px 16px; 
 
    transition: 0.3s; 
 
} 
 

 
/* Change background color of buttons on hover */ 
 
div.tab button:hover { 
 
    background-color: #ddd; 
 
} 
 

 
/* Create an active/current tablink class */ 
 
div.tab button.active { 
 
    background-color: #ccc; 
 
} 
 

 
/* Style the tab content */ 
 
.tabcontent { 
 
    display: none; 
 
    padding: 6px 12px; 
 
    border: 1px solid #ccc; 
 
    border-top: none; 
 
}
<select> 
 
    <option value="" disabled="disabled" selected="selected">Please select name</option> 
 
    <option value="Tom">London</option> 
 
    <option value="Marry">Paris</option> 
 
    <option value="Jane">Tokyo</option> 
 
</select> 
 
<div class="tab"> 
 
    <button class="tablinks" onclick="openCity(event, 'London')">London</button> 
 
    <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button> 
 
    <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button> 
 
</div> 
 

 
<div id="London" class="tabcontent"> 
 
    <h3>London</h3> 
 
    <p>London is the capital city of England.</p> 
 
</div> 
 

 
<div id="Paris" class="tabcontent"> 
 
    <h3>Paris</h3> 
 
    <p>Paris is the capital of France.</p> 
 
</div> 
 

 
<div id="Tokyo" class="tabcontent"> 
 
    <h3>Tokyo</h3> 
 
    <p>Tokyo is the capital of Japan.</p> 
 
</div>

回答

0

會有一些改變,你將不得不在你的HTML和JavaScript代碼來執行。

我做了以下

這些變化//下面是HTML代碼

<select id="change_form_dropdown" onchange="openCity()"> 
    <option value="" disabled="disabled" selected="selected">Please select name</option> 
    <option value="London">London</option> 
    <option value="Paris">Paris</option> 
    <option value="Tokyo">Tokyo</option> 
</select> 
<div class="tab"> 
    <button class="tablinks">London</button> 
    <button class="tablinks">Paris</button> 
    <button class="tablinks">Tokyo</button> 
</div> 

<div id="London" class="tabcontent"> 
    <h3>London</h3> 
    <p>London is the capital city of England.</p> 
</div> 

<div id="Paris" class="tabcontent"> 
    <h3>Paris</h3> 
    <p>Paris is the capital of France.</p> 
</div> 

<div id="Tokyo" class="tabcontent"> 
    <h3>Tokyo</h3> 
    <p>Tokyo is the capital of Japan.</p> 
</div> 

//這裏是JavaScript代碼。

function openCity() { 
    var dropdown_element = document.getElementById("change_form_dropdown"); 
    var cityName = 
      dropdown_element.options[dropdown_element.selectedIndex].value; 

    // Declare all variables 
    var i, tabcontent, tablinks; 

    // Get all elements with class="tabcontent" and hide them 
    tabcontent = document.getElementsByClassName("tabcontent"); 
    for (i = 0; i < tabcontent.length; i++) { 
     tabcontent[i].style.display = "none"; 
    } 

    // Show the current tab, and add an "active" class to the button that 
     opened the tab 
    document.getElementById(cityName).style.display = "block"; 
    //evt.currentTarget.className += " active"; 
}