繁体   English   中英

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

[英]How I set the a field in a input select (HTML) with javascript?

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

将Attribute 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"
}
var randomNumber = 3; // just for example 
document.getElementById('select_field').selectedIndex = randomNumber;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM