selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。

注释:若允许多重选择,则仅会返回第一个被选选项的索引号。

语法

selectObject.selectedIndex=number

实例

下面的例子可提示出被选选项的索引号:

<html>
<head>
<script type="text/javascript">
function getIndex()
{
var x=document.getElementById("mySelect")
alert(x.selectedIndex)
}
</script>
</head>
<body> <form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getIndex()"
value="Alert index of selected option">
</form> </body>
</html> <html>
<head>
<script type="text/javascript">
function insertBeforeSelected()
  {
  var x=document.getElementById("mySelect");
  if (x.selectedIndex>=0)
    {
    var y=document.createElement('option');
    y.text='Kiwi';
    var sel=x.options[x.selectedIndex];  
    try
      {
      x.add(y,sel); // standards compliant
      }
    catch(ex)
      {
      x.add(y,x.selectedIndex); // IE only
      }
    }
  }
</script>
</head>
<body> <form>
<select id="mySelect">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>
<input type="button" onclick="insertBeforeSelected()" value="Insert option before selected" />
</form> </body>
</html>

selectedIndex 属性的更多相关文章

  1. Select selectedIndex 属性

    定义和用法 selectedIndex 属性可设置或返回下拉列表中被选选项的索引号. 注意: 若允许多重选择,则仅会返回第一个被选选项的索引号. 语法 设置 selectedIndex 属性: sel ...

  2. 如何设置 Panorama 控件的只读 SelectedIndex 属性?

    在 OnNavigatedTo() 方法中设置: panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];

  3. selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。

    转自:https://blog.csdn.net/xxj19950917/article/details/73002046

  4. 深入浏览器兼容 细数jQuery Hooks 属性篇

    关于钩子:http://www.cnblogs.com/aaronjs/p/3387906.html 本章的目的很简单,通过钩子函数更细节的了解浏览器差异与处理方案, 版本是2.0.3所以不兼容ie6 ...

  5. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  6. combobox 属性、事件、方法

    一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...

  7. 代码滑动panorama-即程序中设置SelectedIndex

    我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能.那么是不是就没有办法了呢?其实我们可以通过设置SelectedItemProp ...

  8. C#中combobox 控件属性、事件、方法

    一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...

  9. MFC中listbox控件中各种属性的详解

     ListBox控件是Windows 窗体的一个空间,ListBox 控件显示一个项列表,用户可从中选择一项或多项.      如果项总数超出可以显示的项数,则自动向 ListBox 控件添加滚动条. ...

随机推荐

  1. HDU 1014 G题

    Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  2. django JsonResponse和HttpResponse的在后端和前端区别

    JsonResponse和HttpResponse的区别 1.from django.http import JsonResponse return JsonResponse('例子') 2.impo ...

  3. Blender Tutorial - Earth curves [Animation nodes]笔记

    https://www.youtube.com/watch?v=jEYI4lnOprY&t= Blender Tutorial - Earth curves [Animation nodes] ...

  4. php基础-7

    php json数据的读取和转换 将数组转化为json <?php $arr = array('h'=>"hello", "w"=>" ...

  5. (0)前端总结(HTML + CSS + JQ)

    HTML 1.<meta charset="UTF-8">  #设置页面编码,这个设置英文则现在国内浏览器会弹出是否要转换中文 2.<title>我的第一个 ...

  6. docker安装linux系统镜像

    推荐镜像 Centos/Debian/UbuntuCentOS:kinogmt/centos-ssh (默认用户名root,密码password,CentOS6.7)CentOS:tutum/cent ...

  7. linux 之sed

    sed 用法 sed [-nefr] [action] -i 直接修改文件内容,而不是像其他命令那样只是输出到终端 a新增c取代d删除i插入p列印常与sed -n 使用s取代 有一点需要注意的是:如果 ...

  8. MySQL导出用户权限

    在MySQL 5.5/5.6版本中,使用SHOW GRANTS命令可以导出用户的创建脚本和授权脚本. hostname='127.0.0.1' port= username='root' passwo ...

  9. Stateful Kubernetes Applications Made Easier: PSO and FlashBlade

    转自:https://medium.com/@joshua_robinson/stateful-kubernetes-applications-made-easier-pso-and-flashbla ...

  10. mysql杂谈

    本文主要记录一些零碎知识点 1.mysql默认存储引擎变更InnoDB as Default Storage Engine从mysql-5.5.5开始,InnoDB作为默认存储引擎,InnoDB作为支 ...