javascript - 选择标签的填充选项

标签 javascript html

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

我在 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 语句中,选择的国家标签就是我们的条件。 现在,当我运行代码时,第二个 if 不起作用。(它直到第二条语句中的 6075 才显示)并且也不将 auto 选项添加到 select 标记。(我想在第一个选项中显示 auto wirelessChannel 无条件,我的意思是在第一个索引处它应该自动显示) 我哪里做错了?

最佳答案

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

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

关于javascript - 选择标签的填充选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224867/

相关文章:

javascript - 如何在html中上传图片后显示按钮?

javascript - angular2 zone.js 进行自动 sock.js 调用

javascript - 如何在 JavaScript 中创建内存泄漏和监控内存使用情况

html - CSS背景图片未定位底部

javascript - 带有文件上传文本的弹出窗口

html - Bootstrap 3.0.3 form-group 类它不采用任何边距底部

javascript - 未找到模块 - React

javascript - onClick 将图像存储在 HTML5 本地存储中

javascript - slider 无法关闭

javascript - Jquery replacewith 无法正常工作