如何使用javascript以编程方式设置select box元素的值? [英] How do I programmatically set the value of a select box element using javascript?

查看:103
本文介绍了如何使用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;
}

这篇关于如何使用javascript以编程方式设置select box元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆