javascript之HTML(select option)详解

一、基础理解:

var e = document.getElementById("selectId");e. options= new Option("文本","值") ;

//创建一个option对象,即在<select>标签中创建一个或多个<option value="值">文本</option>

//options是个数组,里面可以存放多个<option value="值">文本</option>这样的标签

1:options[ ]数组的属性:

length属性---------长度属性

selectedIndex属性--------当前被选中的框中的文本的索引值,此索引值是内存自动分配的(0,1,2,3.....)对应(第一个文本值,第二个文本值,第三个文本值,第四个文本值..........)

2:单个option的属性(---obj.options[obj.selecedIndex]是指定的某个<option>标签,是一个---)
text属性---------返回/指定 文本

value属性------返回/指定 值,与<options value="...">一致。

index属性-------返回下标,

selected 属性-------返回/指定该对象是否被选中.通过指定 true 或者 false,可以动态的改变选中项

defaultSelected 属性-----返回该对象默认是否被选中。true / false。

3:option的方法

增加一个<option>标签-----obj.options.add(new("文本","值"));<增>

删除一个<option>标签-----obj.options.remove(obj.selectedIndex)<删>

获得一个<option>标签的文本-----obj.options[obj.selectedIndex].text<查>

修改一个<option>标签的值-----obj.options[obj.selectedIndex]=new Option("新文本","新值")<改>

删除所有<option>标签-----obj.options.length = 0

获得一个<option>标签的值-----obj.options[obj.selectedIndex].value

注意:

a:上面的写的是如这样类型的方法obj.options.function()而不写obj.funciton,是因为为了考虑在IE和FF 下的兼容,如obj.add()只能在IE中有效.

b:obj.option中的option不需要大写,new Option中的Option需要大写

二 、应用

<html>

<head>

<script language="javascript">

function number(){

var obj = document.getElementById("mySelect");

//obj.options[obj.selectedIndex] = new Option("我的吃吃","4");//在当前选中的那个的值中改变

//obj.options.add(new Option("我的吃吃","4"));再添加一个option

//alert(obj.selectedIndex);//显示序号,option自己设置的

//obj.options[obj.selectedIndex].text = "我的吃吃";更改值

//obj.remove(obj.selectedIndex);删除功能

}

</script>

</head>

<body>

<select id="mySelect">

<option>我的包包</option>

<option>我的本本</option>

<option>我的油油</option>

<option>我的担子</option>

</select>

<input type="button" name="button" value="查看结果" οnclick="number();">

</body>

</html>

1.动态创建select

function createSelect(){var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}

2.添加选项option

function addOption(){

//根据id查找对象,
var obj=document.getElementById('mySelect');

//添加一个选项
obj.add(new Option("文本","值")); //这个只能在IE中有效
obj.options.add(new Option("text","value")); //这个兼容IE与firefox
}

3.删除所有选项option

function removeAll(){
var obj=document.getElementById('mySelect');

obj.options.length=0;

}

4.删除一个选项option

function removeOne(){
var obj=document.getElementById('mySelect');

//index,要删除选项的序号,这里取当前选中选项的序号

var index=obj.selectedIndex;
obj.options.remove(index);
}

5.获得选项option的值

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index].value;

6.获得选项option的文本

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index].text;

7.修改选项option

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index]=new Option("新文本","新值");

8.删除select

function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect.parentNode.removeChild(mySelect);
}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<head>
<script language=JavaScript>
function $(id)
{
return document.getElementById(id)
}

function show()
{
var selectObj=$("area")
var myOption=document.createElement("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode("上海"))

var myOption1=document.createElement("option")
myOption1.setAttribute("value","100")
myOption1.appendChild(document.createTextNode("南京"))

selectObj.appendChild(myOption)
selectObj.appendChild(myOption1)

}

function choice()
{
var index=$("area").selectedIndex;
var val=$("area").options[index].getAttribute("value")

if(val==10)
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var sh=document.createElement("select")
sh.add(new Option("浦东新区","101"))
sh.add(new Option("黄浦区","102"))
sh.add(new Option("徐汇区","103"))
sh.add(new Option("普陀区","104"))
$("context").appendChild(sh)

}

if(val==100)
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var nj=document.createElement("select")
nj.add(new Option("玄武区","201"))
nj.add(new Option("白下区","202"))
nj.add(new Option("下关区","203"))
nj.add(new Option("栖霞区","204"))
$("context").appendChild(nj)
}
}

function calc()
{
var x=$("context").childNodes.length-1;
alert(x)

}

function remove()
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
}
</script>
<body>

<div id="context">

<select id="area" on

change="choice()">
</select>
</div>
<input type=button value="显示" οnclick="show()">
<input type=button value="计算结点" οnclick="calc()">
<input type=button value="删除" οnclick="remove()">
</body>
</html>

HTML代码:

<TABLE width="100%" border=0 align="left" cellPadding=0 cellSpacing=1>
<tr>
<td class="Search_item_18"> <span class="Edit_mustinput">*</span>引用软件:</td>
<td class="Search_content_82">
<input name="yyrjMc" id="yyrjMc" type="text" class="Search_input" tabindex="3" size="30" >
<input name="yyrjDm" id="yyrjDm" type="hidden" >
<input type="button" class="Search_button_select"
onClick="linkAgeTree('linkage','yyrjtree','yyrjMc','yyrjDm','linkageTree','1');" value="选择...">
</td>
</tr>
<tr>
<td class="Search_item"> <span class="Edit_mustinput">*</span>引用分版:</td>
<td class="Search_content" id="yyfb">
<select name="yyfbDm" style="width:160" id="yyfbDm" onChange="getCpgjThgl('yyfbDm','thgjDm')">

</select>

</td>

</tr>

<tr>

<td class="Search_item">退化构件:</td>

<td class="Search_content" id="thgj">

<select name="thgjDm" style="width:160" id="thgjDm">

<option value="-1" selected>无</option>

</select>

</td>

</tr>

</TABLE>

根据这些东西,自己用JQEURY AJAX+JSON实现了一个小功能如下:

JS代码:(只取了于SELECT相关的代码)
/**
* @description 构件联动下拉列表 (用JQUERY 的AJAX配合JSON实现)
* @prarm selectId 下拉列表的ID
* @prarm method 要调用的方法名称
* @prarm temp 此处存放软件ID
* @prarm url 要跳转的地址
*/
function linkAgeJson(selectId,method,temp,url){
$j.ajax({
type: "get",//使用get方法访问后台
dataType: "json",//返回json格式的数据
url: url,//要访问的后台地址
data: "method=" + method+"&temp="+temp,//要发送的数据
success: function(msg){//msg为返回的数据,在这里做数据绑定
var data = msg.lists;
coverJsonToHtml(selectId,data);
}
});
}

/**
* @description 将JSON数据转换成HTML数据格式
* @prarm selectId 下拉列表的ID
* @prarm nodeArray 返回的JSON数组
*
*/
function coverJsonToHtml(selectId,nodeArray){
//get select
var tempSelect=$j("#"+selectId);
//clear select value
isClearSelect(selectId,'0');
var tempOption=null;
for(var i=0;i<nodeArray.length;i++){
//create select Option
tempOption= $j('<option value="'+nodeArray[i].dm+'">'+nodeArray[i].mc+'</option> ');
//put Option to select
tempSelect.append(tempOption);
}
// 获取退化构件列表
getCpgjThgl(selectId,'thgjDm');
}
/**
* @description 清空下拉列表的值
* @prarm selectId 下拉列表的ID
* @prarm index 开始清空的下标位置
*/
function isClearSelect(selectId,index){
var length=document.getElementById(selectId).options.length;
while(length!=index){
//长度是在变化的,因为必须重新获取
length=document.getElementById(selectId).options.length;
for(var i=index;i<length;i++)
document.getElementById(selectId).options.remove(i);
length=length/2;
}
}

/**
* @description 获取退化构件列表
* @prarm selectId1 引用软件下拉列表的ID
* @prarm selectId2 退化构件下拉列表的ID
*/
function getCpgjThgl(selectId1,selectId2){
var obj1=document.getElementById(selectId1);//引用软件下拉列表
var obj2=document.getElementById(selectId2);//退化构件下拉列表
var len=obj1.options.length;
//当引用软件列表长度等于1时返回,不做操作
if(len==1){
return false;
}
//清空下拉列表的值,两种方式都可以
// isClearSelect(selectId2,'1');
document.getElementById(selectId2).length=1;
for(var i=0;i<len; i++){
var option= obj1.options[i];
//引用软件被选中项不加入
if(i!=obj1.selectedIndex){
//克隆OPTION并添加到SELECT中
obj2.appendChild(option.cloneNode(true));
}
}

}

信很多人在刚接触前端或者中期时候总会遇到一些问题及瓶颈期,如学了一段时间没有方向感或者坚持不下去一个人学习枯燥乏味有问题也不知道怎么解决,对此我整理了一些资料 喜欢我的文章想与更多资深大牛一起讨论和学习的话 欢迎加入我的学习交流群907694362

HTML select option 详解相关推荐

  1. java爬取网页内容 简单例子(2)——附jsoup的select用法详解

    [背景] 在上一篇博文 java爬取网页内容 简单例子(1)--使用正则表达式 里面,介绍了如何使用正则表达式去解析网页的内容,虽然该正则表达式比较通用,但繁琐,代码量多,现实中想要想出一条简单的正则 ...

  2. MySQL基础 , 基本SQL,SELECT查询详解

    定义 MySQL是一个小型开源的关系型数据库管理系统(RDBMS),使用SQL(结构化查询语言)语言进行数据库管理. 优点 1.开源 MySQL是开放源代码的数据库,任何人都可以获得源代码,修正缺陷. ...

  3. mysql查询第八页_第八节:MySQL之Select指令详解和相关练习

    一. 前言 该篇文章基于之前   https://www.cnblogs.com/yaopengfei/p/7182230.html  的基础上进行补充修改. 1. 简介 就查询而言,可以简单的分为: ...

  4. Web基础(从零开始)——HTML下拉菜单 select标签详解

    <select>标签用于定义下拉列表 <from>学院:<select><option>商学院</option><option> ...

  5. elementui下拉框选择图片_Element-UI中Select选择器详解

    image 前言 最近开发的后台管理系统项目采用Vue+Element-UI技术架构,在使用Elment-UI中Select组件的时候遇到了比较多的操作难题,官网上关于这个组件的使用文档介绍的不是很详 ...

  6. JavaScript操作select标签详解

    获取select值 获取显示的汉字 document.getElementById("bigclass").options[window.document.getElementBy ...

  7. mysql select语法_MySQL SELECT语法(一)SELECT语法详解

    SELECT的语法如下: SELECT[ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT ...

  8. mysql select语句详解_mysql学习笔记之完整的select语句用法实例详解

    本文实例讲述了mysql学习笔记之完整的select语句用法.分享给大家供大家参考,具体如下: 本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order ...

  9. select模型详解

    1.select模型原理 使用select函数检查文件描述符上是否有io事件发生,包括可读,可写以及异常 select参数和返回值意义如下: int select ( IN int nfds,     ...

最新文章

  1. python异常类型(Exception只是常规错误的基类)
  2. docker下MySQL修改配置并重启生效:表名不区分大小写
  3. 什么是web标准??
  4. 一个数据仓库转型者眼中的数据挖掘
  5. ECSHOP 订单状态 记录
  6. 摄影测量--相对定向元素与绝对定向元素
  7. 组合模式_Java设计模式-组合模式
  8. 解决git pull 命令失效,不能从远程服务器上拉取代码问题
  9. rust和gta5哪个吃配置_晨报:Steam周销量榜 曝次世代GTA5用大镖客2引擎
  10. php mysql日期区间_php – 3个日期范围之间的Mysql查询
  11. 雷电模拟器android4.2,雷电安卓模拟器-雷电模拟器下载 v4.0.55.0官方版--pc6下载站...
  12. 记一次被动的网卡升级:VMWare导致的无线网卡不能启用
  13. 极简教程!教你快速将K3s与Cloud Controller集成
  14. 别细看|请收藏|堆垛机故障大全及解决办法
  15. SQLserver分离数据库
  16. 微端是什么意思?微端对服务器性能配置要求
  17. EWSTM8系列教程06_工程节点选项配置(一)
  18. 什么是云原生?聊聊云原生的今生
  19. 制作3D动漫模型原来这么简单,会建模还能拿高薪?
  20. 预祝kimi在上海夺冠!

热门文章

  1. C#中调用abobe reader打开PDF文件的方法
  2. 学计算机的女生就业方向知乎,知乎高赞回答:什么样的人适合学计算机?
  3. [分析]欢乐时光源码
  4. 百度AI加速器第七期火热招募
  5. BZOJ4808: 马
  6. windows内外网隔离的环境下同时使用
  7. 基于FPGA的SRIOIP例程及仿真实现
  8. nuxt3 pinia pinia-plugin-persistedstate
  9. 快速获取颜色的RGB或十六进制码(取色技巧)
  10. C/C++中的STL库