对HTML控件select的option的操作(附全码)_AX

以前弄过一次,但没写blog,结果忘了,等想起来,,,等用到了再补上!!
下面介绍的是我昨天对select的操作:
前提:
以下拉框形式显示select,即size="1"(大多以这种形式使用,更符合实际)

取select的时候遇到了点问题,明天接着写.
生气了,,,,用了两个小时调试得下面这段代码服服帖帖的
从中有很多感悟,敬请期待下篇.:【JS调试的总结】


①select.options("id")方法取出一个option
②证明option的索引不能通过option.index来更改其索引值
③通过option的swapNode方法来交换索引
选取一个Item
通过select的selectIndex来选中一个option
如果该Item有id,可以通过id值来选取该Item:  options("hong")

【追加】
ListBox控件的HTML表现形式也为options,如果设置为多选模式.
只能通过遍历来获得每个Item是否被选中,
即select的
selectIndex属性是第一选中的Item的index.也可以遍历每个Item的selected属性.
⑥Item的内容的设置或提取方法有三种
options[i].text==options[i].innerText  /options[i].innerHTML

【追加】过滤符合TextBox内容的Item,为TextBox添加onkeyup事件

None.gif // filter the ListBox's item,Made by AX
None.gif
function  filter_AX()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//要选取的关键字(从第一个字符开始)
InBlock.gif
    var input = document.all(txtFilter).value.toUpperCase();
InBlock.gif    
var inputlen =  input.length;
InBlock.gif    
//lstComputers为ListBox类型
InBlock.gif
    var len = document.all(lstComputers).options.length;
InBlock.gif    
var i;
InBlock.gif    
if (inputlen != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//全部置为未选中状态
InBlock.gif
        for(i = 0; i < len; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
if(document.all(lstFoundComputer).options(i).selected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.all(lstFoundComputer).options(i).selected 
= false;
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
for (i = 0; i < len; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//value与text一致,所以使用value
InBlock.gif
            var strlistitem = document.all(lstComputers).options(i).value.toUpperCase();     
InBlock.gif            
if (strlistitem.substring(0,inputlen) == input)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.all(lstFoundComputer).options(i).selected 
= true;
InBlock.gif                
//只选中一个符合关键字的Item
InBlock.gif
                break;
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}



【全码】

None.gif < head >
ExpandedBlockStart.gifContractedBlock.gif
< script > dot.gif
InBlock.gif
<!--
InBlock.gif
function Start()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif 
//让【洪】作为第一项先显示,看我怎么获取该option
InBlock.gif
 //这种需求应该很多,但在网上我还没搜到过
InBlock.gif
 //注意:有多个id为洪,将返回一个option集合
InBlock.gif
 //id/name不能为汉字,刚才调了半天才找出原因的
InBlock.gif
 var source=document.form1.selTest.options("hong"); 
InBlock.gif alert(
"把【"+source.innerHTML+"】换到最上面");
InBlock.gif
InBlock.gif 
//交换位置
InBlock.gif
 source.swapNode(document.form1.selTest.options(0));
InBlock.gif 
//用[]也可以,Why?
InBlock.gif
 source.swapNode(document.form1.selTest.options[0]);
InBlock.gif 
//日的,还以为没交换成功,原来是选中项没改变,我们让它选中
InBlock.gif
 document.form1.selTest.selectedIndex=0;  
InBlock.gif 
//注:该方法主要是给一个字符串,把与字符串相同的项放到第几个位置.
InBlock.gif
 //刚刚想到可以这样获取该项:
InBlock.gif
 //document.getElementById("hong").innerHTML;
ExpandedSubBlockEnd.gif
}

InBlock.gif
function AX()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
//获取div以便进行显示
InBlock.gif
 var show=document.getElementById("show");
InBlock.gif 
var tempIndex=document.form1.selTest.selectedIndex;
InBlock.gif show.innerHTML
="选中项的索引:"+document.form1.selTest.selectedIndex+"<br>";
InBlock.gif show.innerHTML
+="刚刚选中项的值:(直接取,最简单)"+document.form1.selTest.value+"<br>"
InBlock.gif show.innerHTML
+="刚刚选中项的值:"+document.form1.selTest.options(tempIndex).value+"<br>";
InBlock.gif show.innerHTML
+="刚刚选中项的显示部分(通过索引来,很麻烦):"+document.form1.selTest.options(tempIndex).innerHTML;
InBlock.gif show.innerHTML
+="刚刚选中项的显示部分(通过索引来,后面是text也可以,Why?):"+document.form1.selTest.options(tempIndex).text;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
-->
None.gif
</ script >
None.gif
</ head >
None.gif
< BODY >
None.gif
< FORM  name ="form1" >
None.gif
< div  id ="show" ></ div > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
None.gif
< SELECT  name ="selTest"
None.gifonchange
="javascript:AX();" >
None.gif
< OPTION  VALUE ="AX0" > </ option >
None.gif
< OPTION  id ="hong"  VALUE ="AX1" > </ option >
None.gif
< OPTION  VALUE ="AX2" > </ option >
None.gif
</ SELECT >
None.gif
< input  value ="改变option的顺序"  type ="button"  onClick ="javascript:Start();" >
None.gif
</ FORM >
None.gif
</ BODY >
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值