我如何使用javascript在输入选择(HTML)中设置字段
How I set the a field in a input select (HTML) with javascript?
我将随机选择一个数字,并使用排序的数字在选择中定义所选字段。如何设置字段?
将属性 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;
相关文章:
- 如何设置html元素填充的动画
- 使用 js 将背景图像设置为 HTML
- HTML范围:动态设置值属性
- 无法使用nodeJS在html中设置Image src
- 如何为HTML分配/设置值
- 使用val()在jquery循环中设置html字符串的val
- 使用jQuery按条件将样式设置为html标记
- 使用c#将用户数据设置为HTML标记
- 如何通过内部HTML设置图像
- 使用jQuery.html设置带有提交按钮的表单
- Javascript(jQuery)/HTML:设置<输入>或者<选择>启用,不工作
- 通过HTML设置TinyMCE文本区域的内容
- 将框的内部HTML设置为多个数组之一的元素
- 为表的内部 HTML 设置条件
- 在运行ajax请求时,无法将内部html设置为null
- 如何将javascript中的html设置到元素中的特定位置
- 使用ng-bind-html设置HTML中的Angular绑定
- 通过修改html设置在web表单上上传的默认文件名/目录
- 无法将内部HTML设置为字符串变量
- 从mapbox中的外部HTML设置视图并触发单击标记