填写选择标记的选项

时间:2017-05-28 07:34:01

标签: javascript html

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

我在HTML中这样做了:

select tag

wirelessChanne是必须填写的select标签。 country标记位于if语句中,选定的标记是我们的条件。 现在,当我运行代码时,第二个if不起作用。(它在第二个语句中不会显示到6075)并且也不会向select标签添加auto选项。(我想在第一个选项中显示auto wirelessChannel没有条件,我的意思是在第一个索引它应该显示auto) 我在哪里做错了?

2 个答案:

答案 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 );           
        }
    }
}

答案 1 :(得分:0)

在If语句中使用==代替=。用于向for循环之前的select追加添加选项auto。您已创建该选项但忘记附加选项。使用for selectTag.appendChild(auto); for for循环

 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>