显示:无;在select 8中,在IE 8上不起作用

时间:2013-11-08 10:34:43

标签: javascript html internet-explorer

我是新来的,是一名初级开发人员。我有一个IE8的问题,我的代码在Firefox中工作正常,但在IE中根本不起作用。

我的代码中有2个选择多个,我想在第一个选项中显示第二个选择的一些选项。

HTML:

    <form name="formMenu">
    <table>
    <tr>
        <td align="center">
            <select multiple name="selectOne" title="selectOne" class="selectOne" 
             style="width:180px;height:60px">
                <option value="abc" onclick="Javascript:obtainSecondChoices()">  
                    abc
                </option>
                <option value="def" onclick="Javascript:obtainSecondChoices()">  
                    def
                </option>
            </select>
        </td>
    </tr> 

    <tr>
        <td align="center">
            <select multiple name="selectTwo" title="selectTwo" class="selectTwo"
             style="width:180px;height:60px">
                <option class="abc" value="selTwoOpt1" style="display:none;">
                    selTwoOpt1
                </option>
                <option class="def" value="selTwoOpt2" style="display:none;">
                    selTwoOpt2
                </option>
                <option class="abc" value="selTwoOpt3" style="display:none;">
                    selTwoOpt3
                </option>
            </select>
        </td>
    </tr>
    </table>
    </form>

JavaSript:

    function obtainSecondChoices(){
        var fstSelects = document.formMenu.selectOne;
            for(var i = 0 ; i < fstSelects.length; i++){
                var fstSelect = fstSelects[i];
                if(fstSelect.selected){
                    var scndSelects = document.formMenu.selectTwo;
                    for(var j = 0; j< scndSelects.length; j++){
                    if(scndSelects[j].className == fstSelect.value){
                    scndSelects[j].style.display = '';
                    }
                    }
                } else {
                    var scndSelects = document.formMenu.selectTwo;
                    for(var j = 0; j< scndSelects.length; j++){
                        if(scndSelects[j].className == fstSelect.value){
                            scndSelects[j].style.display = 'none';
                            scndSelects[j].selected = false;
                        }
                }
                }
            }
    }

这应该显示或隐藏和取消选择第二个选项,具体取决于您在第一个选项中选择的内容。

正如我所说,它在Firefox中运行良好,但Internet Explorer最初甚至没有用display:none;

隐藏

提前致谢,抱歉我的英语不好。

2 个答案:

答案 0 :(得分:1)

请尝试使用以下代码:由于使用了临时Select标记,因此您的HTML几乎没有变化。

HTML:

 <form name="formMenu">
    <table>
        <tr>
            <td align="center">
                <select multiple="multiple" name="selectOne" title="selectOne" class="selectOne"
                    style="width: 180px; height: 60px" onchange="obtainSecondChoices()">
                    <option value="abc">abc </option>
                    <option value="def">def </option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="center">
                <select multiple="multiple" name="selectTwo" title="selectTwo" class="selectTwo"
                    style="width: 180px; height: 60px">
                </select>
                <select name="selectTemp" id="selectTemp" style="display: none;">
                    <option class="abc" value="selTwoOpt1">selTwoOpt1 </option>
                    <option class="def" value="selTwoOpt2">selTwoOpt2 </option>
                    <option class="abc" value="selTwoOpt3">selTwoOpt3 </option>
                </select>
            </td>
        </tr>
    </table>
    </form>

JavaScript:

<script type="text/javascript">
        function obtainSecondChoices() {
            var fstSelects = document.formMenu.selectOne;
            var scndSelects = document.formMenu.selectTwo;
            scndSelects.options.length = 0;

            for (var i = 0; i < fstSelects.length; i++) {
                var fstSelect = fstSelects[i];
                if (fstSelect.selected) {
                    var tempSelects = document.formMenu.selectTemp;
                    for (var j = 0; j < tempSelects.length; j++) {
                        if (tempSelects[j].className == fstSelect.value) {
                            scndSelects.options[scndSelects.options.length] = new Option(tempSelects[j].text, tempSelects[j].value);
                        }
                    }
                }
            }
        }
    </script>

答案 1 :(得分:-1)

而不是

scndSelects[j].style.display = 'none';
  scndSelects[j].selected = false;

使用

var ddl = document.getElementById('scndSelects');
  var i;
   i= 5 // no. of option you want to remove
ddl.remove(i);