当前位置: 首页 > news >正文

javascript选择框和选择文本的创建与增加以及设置选中项

<script type="text/javascript">//得到选中项的索引,文本和值函数function getselected(selectedIndex){var selectbox=document.forms[0].elements["location"];var index=selectbox[selectedIndex];var selectedOption=selectbox.options[selectedIndex];console.log("索引:"+index.index +" 文本:"+selectedOption.text+" 值:"+selectedOption.value);}//设置选中项function setOpionSelected(index){var selectbox=document.forms[0].elements["location"];selectbox[index].selected=true;}setOpionSelected(3);//得到选中项的元素function getSelectdOptions(selectbox){var result=new Array();var option=null;for(var i=0,len=selectbox.options.length;i<len;i++){option=selectbox.options[i];if(option.selected){result.push(option);}}return result;}var selected=document.forms[0].elements["location"];//console.log(getSelectdOptions(selected).pop().index);//创建option,这种是用节点的方式var newOption=document.createElement("option");newOption.appendChild(document.createTextNode("option text"));newOption.setAttribute("value","Option value");selected.appendChild(newOption);//创建option,这种是用option对象创建,再用add()函数添加节点,inserBefore()是选择插入位置,如果是插入到最后第2参数直接用"undefined"即可var newOption=new Option("option text","option value");selected.add(newOption,selected.insertBefore(newOption,selected.options[1]));</script>

//html部分

<div id="container"><form action="#" method="post" id="login"><select name="location" id="sel"><option value="sunnyvale, CA">Sunnyvale</option><option value="Los Angeles,CA">Los Angeles</option><option value="Mountain View,CA">Mountain View</option><option value="">China</option><option >Australia</option></select></form></div>
http://www.lryc.cn/news/183725.html

相关文章:

  • 汽车驾驶任务的隐马尔可夫模型识别方法研究
  • Java编程题(完数)
  • 国庆day6
  • 力扣 -- 873. 最长的斐波那契子序列的长度
  • 【程序员必看】计算机网络,快速了解网络层次、常用协议和物理设备!
  • 1.软件测试基础
  • 综合布线系统概述
  • Labview 实战 99乘法表
  • 需求变化频繁的情况下,如何实施自动化测试
  • C++设计模式-桥接(Bridge)
  • Springboot+vue的开放性实验室管理系统(有报告)。Javaee项目,springboot vue前后端分离项目。
  • 1.9.C++项目:仿muduo库实现并发服务器之Connection模块的设计
  • Iphone文件传到电脑用什么软件,看这里
  • JS进阶-原型对象prototype
  • 【Unity】两种方式实现弹跳平台/反弹玩家(玩家触发与物体自身触发事件实现蹦床的物理效果)
  • python开发幸运水果抽奖大转盘
  • 【CMU15-445 Part-16】Concurrency Control Theory
  • 软件设计模式系列之二十五——访问者模式
  • 国庆看坚如磐石
  • 代码随想录Day59 | 647. 回文子串 | 516. 最长回文子序列
  • 为什么InnoDB选择B+树而不是红黑树作为索引结构?
  • 【c++_containers】10分钟带你学会list
  • LeetCode 0714. 买卖股票的最佳时机含手续费
  • cartographer-(0)-ubuntu(20.04)-环境安装
  • MIT 6.S081学习笔记(第二章)
  • L958. 二叉树的完全性检验 java
  • 阿里云对象存储OSS SDK的使用
  • 二、互联网技术——网络协议
  • 初赛错题集
  • Java Thread类详解