将选择标记刷新为默认值

时间:2015-04-01 07:03:43

标签: javascript html

我有2个选择标签。当用户单击第一个选择标记时,我想将第二个选择标记中的值刷新为默认值。我如何在Javascript中实现它

1 个答案:

答案 0 :(得分: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>