我将随机选择一个数字,并在带有排序数字的选择中定义所选字段。我如何设置该字段?
答案 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;