2015-04-01 75 views
-2

我有2個選擇標記。當用戶點擊第一個選擇標記時,我想將第二個選擇標記中的值刷新爲默認值。我如何在Javascript中實現它將選擇標記刷新爲默認值

+2

什麼你第一次嘗試? – 2015-04-01 07:06:02

+0

你可以使用'change'事件的第一個選擇並更新另一個選擇在那個事件 – Sachin 2015-04-01 07:06:33

+0

通過'默認'你的意思是刪除任何動態添加的值或將選定的值改回它的原始值? – Justinas 2015-04-01 07:22:24

回答

0

您將需要使用onchange事件來執行此任務。這個想法是當第一個選擇被改變,你設置了默認值(這裏我選擇設置爲列表的第一項,但你可以做任何你需要重新初始化它的值)。

function initialize() { 
 
    var select_car = document.getElementById("select_car"); 
 
    var select_power = document.getElementById("select_power"); 
 
    
 
    select_car.selectedIndex = 0; 
 
    select_power.selectedIndex = 0; 
 
    
 
    select_car.addEventListener("change", function() { 
 
    select_power.selectedIndex = 1; 
 
    }); 
 
}
<body onload="initialize()"> 
 
<select id="select_car"> 
 
    <option disabled>-- Select a car --</option> 
 
    <option>Mazda MX5</option> 
 
    <option>Nissan Skyline GTR</option> 
 
</select> 
 
<select id="select_power"> 
 
    <option disabled>-- Select a power --</option> 
 
    <option>150CV</option> 
 
    <option>255CV</option> 
 
</select> 
 
</body>