打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
javascript操作Select中的 options集合
object.options.add(newOption(label,value))方法向集合里添加一项option对象;
 
        object.options.remove(index)方法移除options集合中的指定项;
        object.options(index)或options.item(index)可以通过索引获取options集合的指定项;

        select标记还有一个属性为selectedIndex,通过它可能获取当前选择的option索引:object.selectedIndex

         1.清空options集合
            function  optionsClear(object)  
             var length= object.options.length;
                 for(vari=length-1;i>=0;i--){
                    e.options.remove(i);
                }
            

        2.添加一项新option
           function addOption(object)
            object.add(new Option(label,value));
              //使用options集合中最后一项获取焦点
               object.selectedIndex = object.lentht-1;
            }

       3.删除options集合中指定的一项option
          function removeOption(index)
          { if(index >= 0)
              { object.remove(index);
                  //使用options集合中最后一项获取焦点
                 object.selectedIndex = object.lentht-1;
               }
           }

          4.获取当前选定的option的真实值value
             function getCurrentOptionValue(index)
              if(index >= 0)
                   return object(index).value;
              }

          5.获取当前选定的option的显示值label
              functiongetCurrentOptionLabel(index)
              if(index >= 0)
                    return object(index).text;
              }

          下面是日期的联动下拉框:

           function YYYYMMDD(){
               //设置月份的集合
               MonHead   [31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31];  
              //清除年份下拉框的所有内容,最主要包括空格
               var yObj = document.form1.year
               optionsClear(yObj);
              //再给年下拉框赋内容
               yObj.options.add(new Option("请选择",0));
               for (var i =  1950; i<= new Date().getFullYear();i++)  //以1950年为基准,到今年结束 
                    document.form1.year.options.add(new Option(i ,i));  
               //清除月份下拉框的所有内容,最主要包括空格
              var mObj = document.form1.month;
              optionsClear(mObj);
               //再赋月份的下拉框  
              document.form1.month.options.add(newOption("请选择",0));
                for(vari = 1; i < 13;i++)  
                    document.form1.month.options.add(newOption(i,i)); 
                //赋年份的初始值
                document.form1.year.value   document.getElementsByName("YYYY")[0].value;
                //赋月份的初始值 
                document.form1.month.value  document.getElementsByName("MM")[0].value;
               //赋日期下拉框
               var n = MonHead[document.form1.month.value-1];
                if (document.form1.month.value ==2 && IsPinYear(YYYYvalue))  n++;  
                writeDay(n);  
                //赋日期的初始值
                document.form1.date.value =document.getElementsByName("DD")[0].value;  
          }

           年发生变化时日期发生变化(主要是判断闰平年)
           
function YYYYDD(str)  
                var MMvalue =document.form1.month.options[document.form1.month.selectedIndex].value; 
                if  (MMvalue == "") { 
                     var e =document.form1.date;  
                     optionsClear(e);   
                      return;
                  
                var n = MonHead[MMvalue - 1]; 
                if (MMvalue == 2&&IsPinYear(str))  n++;  
                writeDay(n)
           

          月发生变化时日期联动
          
function MMDD(str)   
               varYYYYvalue=document.form1.year.options[document.form1.year.selectedIndex].value;  
              if  (YYYYvalue ==""){  
                  var e =document.form1.date;  
                  optionsClear(e);  
                  return;
               
              var n = MonHead[str -1];   
              if  (str == 2 &&IsPinYear(YYYYvalue))  n++;  
              writeDay(n)   
          }

         据条件写日期的下拉框
         
function  writeDay(n){   
             vare= document.form1.date;  
            optionsClear(e);  
            for (var i=1; i<(n+1);i++)  
            e.options.add(new  Option(i,i));  
         }

         判断是否闰平年
         function  IsPinYear(year){     
             return(0 == year%4 && (year%100 !=0|| year%400 == 0));
        }
    

        清除下拉框的所有内容
        
function  optionsClear(e) {  
             var length = object.options.length;
             for(var i=length-1;i>=0;i--){
                 e.options.remove(i);
            }

       }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
shithappen的博客
用js实现省市县联动
ASP.NET中如何结合JS,在列表框中作多项选择
js出生日期 年月日级联菜单
javascript表单验证大全,
JavaScript控制下拉框(Select)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服