我如何使用javascript在输入选择(HTML)中设置字段?

时间:2011-05-10 17:45:43

标签: javascript html select field

我将随机选择一个数字,并在带有排序数字的选择中定义所选字段。我如何设置该字段?

2 个答案:

答案 0 :(得分:3)

将selected selected =“selected”添加到要选择的选项元素中。

伪代码

function foo()
{
create list of option elements
generate random number
get a single option element at index of random number
set all options attribute "selected" = null
set the option element[at index of random number] attribute selected = "selected"
}

答案 1 :(得分:1)

var randomNumber = 3; // just for example 
document.getElementById('select_field').selectedIndex = randomNumber;