2017-05-28 128 views
1

在我的HTML文件中,我有一个select tag,其中当我选择第一个时,另一个select tag应填写一些选项,当选择其他选项时,其他一些选项将填写另一个选项select tag选择标记的填充选项

我这样做,在我的HTML:

 <script type="text/javascript"> 
     function fillWirelessChannel(){ 
      var index; 
      var selectTag= document.getElementById('wirelessChannel');      selectTag.options.length = 0; // wireless should 
      selectTag.options.length = 0; 
      var auto = document.createElement("option"); 
      auto.value= 'auto'; 
      auto.innerHTML = 'auto'; 
      if (document.getElementById('country').selected="1") { 
        for(index=4920; index<=5825; index+=5){ 
         var opt = document.createElement("option"); 
         opt.value= index; 
         opt.innerHTML = index;  
         selectTag.appendChild(opt);   
        } 
       } else if(document.getElementById('country').selected="2"){    
        for(index=4920; index<=6075; index+=5){ 
         var opt = document.createElement("option"); 
         opt.value= index; 
         opt.innerHTML = index;  
         selectTag.appendChild(opt);   
        } 
       } 
     } 
    </script> 

的wirelessChanne是选择标签必须填写。 国家/地区标签位于if语句中,选定的是我们的条件。 现在,当我运行代码时,第二个如果不起作用(它在第二个语句中不会显示到6075),也不会将自动选项添加到选择标记(我想在第一个选项中显示auto无线信道没有条件,我的意思是在第一个索引它应该显示自动) 我在哪里做错了?

+0

你永远不会加入*自动*到* selectTag * – ControlAltDel

+2

您正在使用单个'='逻辑测试的一件事情,您应该使用双'==' – RamRaider

+0

选项元素不包含标记,因此设置其* innerHTML *属性是不合适的。改为设置* text *属性。 “标签”属于标记,DOM包含元素。 – RobG

回答

0

在使用if语句==,而不是=。添加一个选项来自动选择追加,你对loop.You创建的选项,但之前忘了的选项。使用selectTag.appendChild(auto);追加以前生产的循环

fillWirelessChannel(); 
 

 
function fillWirelessChannel(){ 
 
      var index; 
 
      var selectTag= document.getElementById('wirelessChannel');      selectTag.options.length = 0; /* wireless should */ 
 
      selectTag.options.length = 0; 
 
      var auto = document.createElement("option"); 
 
      auto.value= 'auto'; 
 
      auto.innerHTML = 'auto'; 
 
      selectTag.appendChild(auto);  
 
      console.log(document.getElementById('country').value); 
 
      
 
      if (document.getElementById('country').value=="1") { 
 
        for(index=4920; index<=5825; index+=5){ 
 
         var opt = document.createElement("option"); 
 
         opt.value= index; 
 
         opt.innerHTML = index;  
 
         selectTag.appendChild(opt);   
 
        } 
 
       } else if(document.getElementById('country').value=="2"){    
 
        for(index=4920; index<=6075; index+=5){ 
 
         var opt = document.createElement("option"); 
 
         opt.value= index; 
 
         opt.innerHTML = index;  
 
         selectTag.appendChild(opt);   
 
        } 
 
       } 
 
     }
<select id="wirelessChannel"> 
 
</select> 
 

 
<select id="country" onchange="fillWirelessChannel()"> 
 
<option value="1">1</option> 
 
<option value="2">2</option> 
 
</select>

+0

谢谢你的回应先生。 是的,你是对的。 再次感谢 :-) –

1

测试值是否等于您需要使用双等号的值 - 将值设置为您将使用单个等号的值。

function fillWirelessChannel(){ 
    var index; 

    var selectTag= document.getElementById('wirelessChannel'); 
     selectTag.options.length = 0; 

    var auto = document.createElement('option'); 
     auto.value= 'auto'; 
     auto.text = 'auto'; 

    selectTag.appendChild(auto); 

    var oCountry=document.getElementById('country'); 

    if (oCountry.value==1) { 
     for(index=4920; index <= 5825; index+=5){ 
      var opt = document.createElement('option'); 
       opt.value= index; 
       opt.text = index; 

      selectTag.appendChild(opt);   
     } 
    } else if(oCountry.value=='2'){    
     for(index=4920; index <= 6075; index+=5){ 
      var opt = document.createElement('option'); 
       opt.value= index; 
       opt.text = index; 

      selectTag.appendChild(opt);   
     } 
    } 
} 
+0

没有一个很好的理由使用* selectedIndex *,因为一些古老的Netscape Navigator版本,'oCountry.value =='1''就足够了。 – RobG

+0

好的 - 我不知道,我一直使用帽子的语法(自NN的日子以来),所以现在我可以缩短我的代码 - 谢谢@RobG – RamRaider

+0

有趣的是,这些旧的模因依然存在。 ;-) – RobG