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 ...
随机推荐
- JavaScript头像上传器的实现
最近做这方面的东西,刚开始准备用一个开源项目:https://github.com/yueyoum/django-upload-avatar 后来发现这个开源组件的原设计者的定制化选项设计略显复杂,发 ...
- No module named zope.interface error的解决
明明安装了 zope.interface,还是出现标题错误,错误语句是 from zope.interface import ooxx 根据 http://stackoverflow.com/ques ...
- Linq小整理
Linq(Language Integrated Query)中文翻译为语言集成查询 (1)源起 .net的设计者在类库中定义了一系列的扩展方法 来方便用户操作集合对象 这些扩展方法构成了LINQ的查 ...
- Java容器:Stack,Queue,PriorityQueue和BlockingQueue
Stack Queue PriorityQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue PriorityBlockingQueue ...
- 为什么「margin:auto」可以让块级元素水平居中?
知乎链接:http://www.zhihu.com/question/21644198 关于BFC的解释:W3CFans http://www.w3cfuns.com/thread-5595727-1 ...
- windows下virtualenv中安装MySQL-python
先在正常的环境下安装 MySQL-python-1.2.3.win-amd64-py2.7.exe (用everything搜索一下就出来) 然后到 C:\Python27\Lib\site-pack ...
- Java面向对象(二、继承)
Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法 ...
- 基于ubuntu16.04快速构建Hyperledger Fabric网络
前言 最近在参加一个比赛,使用到了区块链的开源软件hyperledger,由于之前从未接触过区块链,以及和区块链开发相关的内容,所有在网上查阅了大量的资料,并且通过学习yeasy(杨宝华)开源的入门书 ...
- Java操作Memcached
本文复制其他播客,有好的技术文章希望各位大神能告知... 谢谢. 如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; import j ...
- PHP入门小练习
1.编写字符串检查函数,判断一个字符串是否为有效电话号码.要求:手机号码的长度为11位的数字,固定电话为开头三或四个数字后跟一个短横线后接8位数字. <? function isTel($tel ...