javascript Select标记中options操作方法集合_表单特效

javascript操作Select标记中options集合
先来看看options集合的这几个方法:
options.add(option)方法向集合里添加一项option对象;
options.remove(index)方法移除options集合中的指定项;
options(index)或options.item(index)可以通过索引获取options集合的指定项;

javascript代码如下:

var selectTag = null; //select标记
var OPTONLENGTH = 10; //每次填充option数
var colls = []; //对select标记options的引用

window.onload = function(){
selectTag = document.getElementById("SelectBox"); //获取select标记
colls = selectTag.options; //获取引用
//initSelectBox(); //自初始化select.options
};

//使用随机数填充select.options
function initSelectBox(){
var random = 0 ;
var optionItem = null;
var item = null;

if(colls.length > 0 && isClearOption()){
clearOptions(colls);
}

for(var i=0;i<OPTONLENGTH;i++){

random = Math.floor(Math.random()*9000)+1000;
item = new Option(random,random); //通过Option()构造函数创建option对象
selectTag.options.add(item); //添加到options集合中
}

watchState();
}
//添加新option项前是否清空当前options
function isClearOption(){
return document.getElementById("chkClear").checked;
}
//清空options集合
function clearOptions(colls){
var length = colls.length;
for(var i=length-1;i>=0;i--){
colls.remove(i);
}
}
//添加一项新option
function addOption(){
colls.add(createOption());
lastOptionOnFocus(colls.length-1);
watchState();
}
//创建一个option对象
function createOption(){
var random = Math.floor(Math.random()*9000)+1000;
return new Option(random,random);
}
//删除options集合中指定的一项option
function removeOption(index){
if(index >= 0){
colls.remove(index);
lastOptionOnFocus(colls.length-1);
}
watchState();
}
//获取当前选定的option索引
function getSelectedIndex(){
return selectTag.selectedIndex;
}
//获取options集合的总数
function getOptionLength(){
return colls.length;
}
//获取当前选定的option文本
function getCurrentOptionValue(index){
if(index >= 0)
return colls(index).value;
}
//获取当前选定的option值
function getCurrentOptionText(index){
if(index >= 0)
return colls(index).text;
}
//使用options集合中最后一项获取焦点
function lastOptionOnFocus(index){
selectTag.selectedIndex = index;
}
//显示当select标记状态
function watchState(){
var divWatch = document.getElementById("divWatch");
var innerHtml="";
innerHtml = "option总数:" + getOptionLength();
innerHtml += "<br/>当前项索引:" + getSelectedIndex();
innerHtml += "<br/>当前项文本:" + getCurrentOptionText(getSelectedIndex());
innerHtml += "<br/>当前项值:" + getCurrentOptionValue(getSelectedIndex());
divWatch.innerHTML = innerHtml;
divWatch.align = "justify";
}

注意到上面创建option项时,使用了Option()构造函数,这个构造函数有两个版本的重载。
1、var option = new Option(text,value); //这里要大写Option()
2、var option = new Option();
option.text = text;
option.value=value;
我个人比较喜欢第一种方法来创建option对象。
另外,select标记还有一个比较有用的属性就是selectedIndex,通过它可能获取当前选择的option索引,或通过索引设置指定options集合中哪一项被选择。
select.selctedIndex = select.options.length-1; //将options集合中最后一项选中
var selectedItem = select.options(select.selectedIndex);//获取当前选中项
selectedItem.text; //选中项的文本
selectedItem.value; //选中项的值

<BODY>
<Select name="SelectBox">
</Select>
<hr/>
<div id="divWatch" style="background-color:beige;width=220;">
</div>
<hr/>
<h4>使用随机数初始化SelectBox</h4>
<input type="button" value="Init" onclick="initSelectBox()"/> <input type="checkbox" name="chkClear"/>clear
<hr/>
<h4>添加option项</h4>
<input type="button" value="create" onclick="addOption()"/>
<hr/>
<h4>删除option项</h4>
<input type="button" value="delete" onclick="removeOption(colls.length-1)"/>
</BODY>
检测是否有选中
if(objSelect.selectedIndex > -1) {
//说明选中
} else {
//说明没有选中
}

删除被选中的项
objSelect.options[objSelect.selectedIndex] = null;

增加项
objSelect.options[objSelect.length] = new Option("你好","hello");

修改所选择中的项
objSelect.options[objSelect.selectedIndex] = new Option("你好","hello");

得到所选择项的文本
objSelect.options[objSelect.selectedIndex].text;

得到所选择项的值
objSelect.options[objSelect.selectedIndex].value;

时间: 2024-09-20 06:28:46

javascript Select标记中options操作方法集合_表单特效的相关文章

javascript select 之间传值效果的代码_表单特效

二个框内值传递.不错的效果 复制代码 代码如下: <script type="text/javascript" defer="defer">  function $(value)  {   return document.getElementById(value);  }  function ch(s){   var p = new Array(0,1,2);   p[0] = '大学';   p[1] = '高中';   p[2] = '初中';   

Javascript select下拉框操作常用方法_表单特效

复制代码 代码如下: function AddDropDownList(id,fatherCtl) { if(!document.getElementById(id)) { var ddl = document.createElement('select'); ddl.setAttribute("id",id); if(fatherCtl&&document.getElementById(fatherCtl)) document.getElementById(fathe

JavaScript聚焦于第一个字段的代码_表单特效

这个主要会用在表单字段中,所以我这里做的实验只针对表单字段.在form的子元素中循环查找第一个不是隐藏字段的字段,然后使用focus方法获得焦点.(呵呵^_^,很简单!) 复制代码 代码如下: FormUtil.focusOnFirst = function() { if (document.forms.length > 0) { for (var i = 0; i < document.forms[0].elements.length; i++) { var oField = documen

Javascript Select操作大集合_表单特效

其实这本书一直都在我的电脑里,只是没认真看过.一直没怎么很正式的学习过javascript,偶尔用到的时候就到网上找些代码,改吧改吧就用了,这次从头开始学起,细细看下来,还真是有不少收获,甚至有点喜欢上javascript了. 现在步入正题,看到书中讲Form元素的操作,像Textbox.Button.Label等,都还是比较简单的,只是看到Select时,稍微有些复杂,于是就想仔细研究研究,于是就有了这篇文章.Select的操作包括动态添加.删除.移动.获取选中项的值.排序等等,现在一一讲述.

javascript 操作select下拉列表框的一点小经验_表单特效

按照我一贯的web开发风格,所有不直接操作数据库的事件,都尽可能由javascript来实现,所以这个需求我打算使用js来完成. 首先来分析一下具体情况:这个页面是一个更新页面,品牌有品牌1和品牌2两个字段,品牌2可以为空,品牌1不能为空,所以品牌2的下拉列表框比品牌1多一项:如果选择了品牌的前8相中的任意一项,"活跃状态"要隐藏,否则"活跃状态"默认显示状态为"潜在":当查询的结果品牌1和品牌2有任意一项在品牌的前8相中,"活跃状态&

用javascript来实现select标签的美化的代码_表单特效

用javascript模拟select达到美化效果 用户注册 帐号 密码 省份 江西福建广东浙江 http://www.jb51.net/article/http://www.iwcn.net">作者博客

javascript之dhDataGrid Ver2.0.0代码_表单特效

针对dhDataGrid的前一版本Ver1.0.0而做的更新! 1.标题栏.左边边栏固定方式更新,取消expression方式: 2.排序方式更新,同时支持数字.字符.日期.数字字符混合.中文汉字的排序: 3.支持换肤,您可以自己定制该控件样式: 4.预留[双击].[右键]功能: 5.支持IE.FF: CSS: /*dhdatagrid 大块样式*/ #dhdatagrid {position:relative;width:500px;height:200px;background:white;

JS 无限级 Select效果实现代码(json格式)_表单特效

演示地址: http://demo.jb51.net/js/2011/js_select/index.htm数据 复制代码 代码如下: var data=[ {id:1,name:"前端开发",pid:0}, {id:2,name:"CSS",pid:1}, {id:3,name:"JS",pid:1}, {id:4,name:"HTML",pid:1}, {id:5,name:"数据库",pid:0},

JavaScript动态调整TextArea高度的代码_表单特效

核心代码: 复制代码 代码如下: <script language="javascript" type="text/javascript"> function adjustObjHeight(obj, defaultHeight) { if(obj.scrollHeight > defaultHeight) { obj.style.height = obj.scrollHeight + 'px'; } else { obj.style.height