HTML中的<select>标签如何设置默认选中的选项
方法有两种。
第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果。
|
1
2
3
4
5
|
< select id = "sel" >< option value = "1" >1</ option >< option value = "2" selected = "selected" >2</ option >< option value = "3" >3</ option ></ select > |
第二种为通过前端js来控制选中的项: 由 www.169it.com 搜集整理
|
1
2
3
4
5
6
7
8
9
10
11
|
< script type = "text/javascript" >function change(){ document.getElementById("sel")[2].selected=true;}</ script >< select id = "sel" >< option value = "1" >1</ option >< option value = "2" >2</ option >< option value = "3" >3</ option ></ select >< input type = "button" value = "修改" onclick = "change()" /> |
获取<select>标签选中项文本的js代码为:
|
1
2
|
var val = document.all.Item.options[document.all.Item.selectedIndex].textvar i=document.getElementById( 'sel' ).options[document.getElementById( 'sel' ).selectedIndex].value; |
一些其它操作<select>标签的技巧如下:
1)动态创建select
|
1
2
3
4
5
|
function createSelect(){var mySelect = document.createElement( "select" );mySelect.id = "mySelect" ;document.body.appendChild(mySelect);} |
2)添加选项option
|
1
2
3
4
5
6
|
function addOption(){//根据id查找对象,var obj=document.getElementById( 'mySelect' );//添加一个选项obj.add( new Option( "文本" , "值" ));} |
3)删除所有选项option
|
1
2
3
4
|
function removeAll(){var obj=document.getElementById( 'mySelect' );obj.options.length=0;} |
4)删除一个选项option
|
1
2
3
4
5
6
|
function removeOne(){var obj=document.getElementById( 'mySelect' );//index,要删除选项的序号,这里取当前选中选项的序号var index=obj.selectedIndex;obj.options.remove(index);} |
5)获得选项option的值
|
1
2
3
|
var obj=document.getElementById( 'mySelect' );var index=obj.selectedIndex; //序号,取当前选中选项的序号var val = obj.options[index].value; |
6)获得选项option的文本
|
1
2
3
|
var obj=document.getElementById( 'mySelect' );var index=obj.selectedIndex; //序号,取当前选中选项的序号var val = obj.options[index].text; |
7)修改选项option
|
1
2
3
|
var obj=document.getElementById( 'mySelect' );var index=obj.selectedIndex; //序号,取当前选中选项的序号var val = obj.options[index]= new Option( "新文本" , "新值" ); |
8)删除select
|
1
2
3
4
|
function removeSelect(){var mySelect = document.getElementById( "mySelect" );mySelect.parentNode.removeChild(mySelect);} |
HTML中的<select>标签如何设置默认选中的选项的更多相关文章
- vue中select设置默认选中
vue中select设置默认选中 一.总结 一句话总结: 通过v-model来:select上v-model的值为option默认选中的那项的值(value) 二.select设置默认选中实例 < ...
- Android WebView的HTML中的select标签不起作用
Android WebView的HTML中的select标签不起作用 经过查询资料,了解到android对html里的select标签是弹出一个原生的选择器. 问题: Webview中的select没 ...
- Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法
项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . .. 我开始这样给设置默认选中一个的: for (int ...
- winform中的dateTimePicker控件设置默认值为空
winform中的dateTimePicker控件设置默认值为空 第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键
- Django forms 关于select和checkbox设置初始选中值及让前端选中指定值
Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...
- Django forms 关于select和checkbox设置初始选中值
Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...
- Vue Echarts 饼图设置默认选中一个
Vue Echarts 饼图设置默认选中一个 myChart.setOption(data) // data伟echarts所需要传入的参数,就是配置参数最多的那个玩意 myChart.dispatc ...
- echarts圆饼图设置默认选中项并在中间显示文字
效果: 代码: var myChart = echarts.init(document.getElementById('quanshi-echarts-two')); option = { grid: ...
- html select 标签设置默认选中
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...
随机推荐
- 网络复习之TCP
可靠传输的工作原理 1 停止等待协议 每发送完一个分组,就停止发送,等待对方确认.出现差错,超时重传. 1.1 暂时保留已发送的分组的副本 1.2 分组和确认分组必须进行编号 ...
- MEF 编程指南(八):过滤目录
当使用子容器的时候,基于特定的标准(Specific Criteria)过滤目录是很必要的.比如,基于部件构造策略的过滤器是很常见的.下面的代码片段演示了如何构建的特殊途径(Particular Ap ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- 创建虚拟交换机(New-VMSwitch)
#获取网卡列表Get-NetAdapter
- Android学习笔记(2)
今天我继续看Mars老师的Android开发视频教程,看到一个“深入LinearLayout”的时候,发现一个比较好玩的技巧. 控件的layout_weight属性,他是父控件剩余空间的比例. 如果把 ...
- cdoj 1253 阿里巴巴和n个大盗 博弈论
阿里巴巴和n个大盗 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1253 D ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- [React] Styling React Components With Aphrodite
Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...
- __attribute__机制介绍
转载:http://blog.csdn.net/ithomer/article/details/6566739 1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__att ...
- SQL Server 之 在数据库之间进行数据导入导出
1.同一服务器上数据库之间进行数据导入导出 (1).使用 SELECT INTO 导出数据 在SQL Server中使用最广泛的就是通过SELECT INTO语句导出数据,SELECT INTO语句同 ...