如何使用javascript以编程方式设置select box元素的值?

问题描述:

我有以下HTML < select> 元素:

I have the following HTML <select> element:

<select id="leaveCode" name="leaveCode">
  <option value="10">Annual Leave</option>
  <option value="11">Medical Leave</option>
  <option value="14">Long Service</option>
  <option value="17">Leave Without Pay</option>
</select>

使用JavaScript函数与 leaveCode 号作为参数,如何在列表中选择适当的选项?

Using a JavaScript function with the leaveCode number as a parameter, how do I select the appropriate option in the list?

function SelectElement(valueToSelect)
{    
    var element = document.getElementById('leaveCode');
    element.value = valueToSelect;
}