用javascript动态更改DropDownList下拉项

 

function  clearitem(){ 

var  drp1  =  document.getElementById( " drp1 " );
        
while (drp1.options.length > 0 )
        {
           drp1.options.remove(
0 );
        } 

}

// 动态更改方法(根据城市代码取得该市商业区并添加到DropDownList中)

 
function  getsyq()
    {
        
var  city  =  document.getElementById( " DropDownList_Cities " ).value;   // 取得城市代码
         var  htp  =   new  ActiveXObject( " Msxml2.XMLHTTP " );
        
var  drp1  =  document.getElementById( " drp1 " ); 
        
var  url  =   " ?stat=1&city= " + city  
        htp.open(
" post " ,url, true )
        htp.onreadystatechange
= function ()
        {
            
if (htp.readyState == 4 )
            {

   clearitem();              
// 清除原有下拉项
                var  str  =  htp.responseText;
               
var  opt  =  str.split( ' , ' );
               
var  s  =  opt.length
                
for ( var  j  =   0 ;j < s;j ++ )
               { 
                  
var  newOption    =   document.createElement( " OPTION " );    // 定义一个新的项
                   var  ff  =  opt[j].split( ' | ' );  
                newOption.text  
=   ff[ 1 ];   
                newOption.value 
=   ff[ 1 ];   
                drp1.options.add(newOption);  
               }  
            }
        }
        htp.send()
        
    }

你可能感兴趣的:(用javascript动态更改DropDownList下拉项)