js 添加select option 和取值

Js代碼

function btnAdd_onclick()"   
{   
   var opt=new Option('New York','1');   
   document.fm.citys.options[0]=opt;   
}   
<form id="fm" name="fm">   
...   
     <select name="citys" id="citys">

 

刪除select中的某一項option: 
      document.getElementById("citys").options.remove(indx);  

刪除select中的全部options: 
       document.getElementById("citys").options.length=0; 

添加select中的項option: 
       document.getElementById("citys").options.add(new Option(text,value));

取值:java

function getvalue()    
    {    
         var obj = document.getElementById('citys');   
         var m=obj.options[obj.selectedIndex].value    
         alert(m);//獲取value    
         var n=obj.options[obj.selectedIndex].text    
         alert(n);//獲取文本    
     }
相關文章
相關標籤/搜索