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 标签设置默认选中的更多相关文章
- HTML中的<select>标签如何设置默认选中的选项
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...
- vue中select设置默认选中
vue中select设置默认选中 一.总结 一句话总结: 通过v-model来:select上v-model的值为option默认选中的那项的值(value) 二.select设置默认选中实例 < ...
- Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法
项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . .. 我开始这样给设置默认选中一个的: for (int ...
- Vue Echarts 饼图设置默认选中一个
Vue Echarts 饼图设置默认选中一个 myChart.setOption(data) // data伟echarts所需要传入的参数,就是配置参数最多的那个玩意 myChart.dispatc ...
- echarts圆饼图设置默认选中项并在中间显示文字
效果: 代码: var myChart = echarts.init(document.getElementById('quanshi-echarts-two')); option = { grid: ...
- vue中select的使用以及select设置默认选中
简介 今天写pc端引入vue,遇到了一个问题,就是我循环出select内的数据以后,发现原本默认显示第一条的select框变成了空白,要选择后才有显示,结果查了好多文档,讲的都不是很清楚,后来看到一句 ...
- 如何修改select标签的默认下拉箭头样式?
对于一般的项目而言,select标签在浏览器中表现出来的默认样式也不算丑,但是一次项目中,项目经理却要求对select标签本身进行样式修改,美化其下拉小箭头的样式.我思考和尝试了许多方法,最终得到一种 ...
- Mysql select语句设置默认值
1.在没有设置默认值的情况下: SELECT userinfo.id, user_name, role, adm_regionid, region_name , create_time FROM us ...
- Ant Design Vue select下拉列表设置默认值
在项目中需要为Ant Design Vue 的 select 组件设置一个默认值,如下图所示的状态下拉选择框,默认选择全部 代码如下: <a-select v-model="query ...
随机推荐
- mysql cluster部署
一.mysql cluster的基本概念 1.mysql cluster的组成 管理(MGM)节点:这类节点的作用是管理MySQL Cluster内的其他节点,如提供配置数据.启 ...
- Universal USB Installer – Easy as 1 2 3
Universal USB Installer aka UUI is a Live Linux Bootable USB Creator that allows you to choose from ...
- Java c# 跨语言Json反序列化首字母大小写问题
C#标准是首字母大写,Java规范是首字母小写,在序列化成Json之后,反序列化会出现反序列化失败的问题.. 从C#反序列化成JavaBean的时候通过如下注解可以直接解决该问题 @JsonNamin ...
- cocos2d-x_ Windows下Android环境搭建
在Windows环境下编译cocos2d-x-3.0 Android-NDK编译:cocos2d-x(二) Mac 下搭建:http://www.cocoachina.com/bbs/read.php ...
- Bagging与方差
在集成学习中,通常认为Bagging的主要作用是降低方差,而Boosting的主要作用是降低偏差.Boosting能降低偏差很好理解,因为其原理就是将多个弱学习器组合成强学习器.但Bagging为什么 ...
- Python_eval()
''' eval()用来把任意字符串转化为Python表达式并进行求值 ''' print(eval('3+4')) #计算表达式的值 a=3 b=4 print(eval('a+b')) #这时候要 ...
- 【转】怎么解决java.lang.NoClassDefFoundError错误 ,以及类的加载机制
转自http://blog.csdn.net/jamesjxin/article/details/46606307 前言 在日常Java开发中,我们经常碰到java.lang.NoClassDefFo ...
- Linux时间子系统之五:低分辨率定时器的原理和实现
专题文档汇总目录 Notes:低精度timer在内核中的数据结构以及API接口:低精度timer精巧高效的分组,使用cascade进行定时器移位,组内Timer FIFO:低精度Timer的初始化流程 ...
- IntelliJ IDEA(九) :酷炫插件系列
最近项目比较忙,很久没有更新IDEA系列了,今天介绍一下IDEA的一些炫酷的插件,IDEA强大的插件库,不仅能给我们带来一些开发的便捷,还能提高我们的与众不同. 1.插件的安装 打开setting文件 ...
- 2.Spring构建REST Web Service
上篇文章我们已经对Spring 已经有了一个初步的认识,接下来本篇文章我们将继续一起在官网学习新技术. 原文地址:https://spring.io/guides/gs/rest-service/ 本 ...