space line
space line
space line
space line
space line
您现在的位置是: Javascript基础>>

JS动态操作Select的函数

发布日期 2008-7-7 22:43:51
作者 shemily
出处 重用网
浏览次数 ...
今日浏览 ...
本月浏览 ...
我要留言 我要留言
space line
摘要
针对Select(HTML)标签的几个Javascript动态操作函数
space line
详细内容

函数体如下:

//------select options control-----
function addOptions(selectId, arrValue, arrText){
 try{
  for(i = 0, arrLen = arrValue.length; i < arrLen; i++){
   if(arrText[i] != "" && arrValue[i] != ""){
    $(selectId).options.add( new Option(arrText[i], arrValue[i]) );
   }
  }
 }catch(e){}
}  //添加option

function clearOptions(selectId){
 try{
  $(selectId).options.length=0;
 }catch(e){}
}  //清楚option

function setSelected(selectId, index){
 try{
  $(selectId).options[index].selected = true;
 }catch(e){}
}  //根据序号设置选项选中

function setSelectedByValue(selectId, selectValue){
 try{
  for(var i=0; i<$(selectId).options.length; i++ ){
   if($(selectId).options[i].value == selectValue){
    $(selectId).options[i].selected = true;
   }else{
    $(selectId).options[i].removeAttribute("selected");
   }
  }
 }catch(e){}
}  //根据option的value值设置选项选中

function getSelectValue(selectId){
 try{
  return $(selectId).options[$(selectId).selectedIndex].value;
 }catch(e){return null;}
}  //获取选中的选项的value值

function getSelectText(selectId){
 try{
  return $(selectId).options[$(selectId).selectedIndex].text;
 }catch(e){return null;}
}  //获取选中选项的text值

 

space line
我要留言 我要留言    
space line
space line
space line
space line
赞助商提供
space line
space line