繁体   English   中英

JS在IE上工作,不适用于chrome和FF

[英]JS that work on IE and doesn't work on chrome and FF

好吧,这确实是一个非常奇怪的问题,但它确实发生在我身上,我写了一个可以在IE上工作的JS,它不在firefox或chrome上。

我希望有人可以告诉我为什么会发生这种情况......

我的HTML代码是下一个

<h4>What kind of animals do you like the most?</h4>
<select id="animalName" name="animalName">
    <option selected="selected" disabled="disabled">Which animal is it?</option>
    <option value="Cat">Cat</option>
    <option value="Dog">Dog</option>
    <option value="Lion">Lion</option>
    <option value="Other">Other</option>
</select>
<div id="otherAnimalNameDiv">
    <h4>As you picked the other, would you like to write it down to let us know?</h4>
    <input type="text" name="otherAnimalName" size="40" id="otherAnimalName" />
</div>

我的JS代码是下一个:

    var animalName = document.getElementById("animalName");
    var animalOtherName = document.getElementById("otherAnimalNameDiv");

    function animalSelect (){
        animalName.onchange = function (){
            if (animalName.options.value == "Other"){
                animalOtherName.style.display = "block";
            }
        };
        animalOtherName.style.display = "none";
    }

window.onload = function () {
    firstNameFunc();
    familyNameFunc ();
    emailAddressFunc ();
    passwordFunc ();
    rewritePasswordFunc ();
    colorPickerFunc ();
    animalSelect ();
    }   

为什么它会在IE上工作而不能在Chrome或FF上工作?....

当你从select标签中选择“Other”选项时,它应该将div显示设置为阻止,这种情况发生在IE上但不是FF或chrome。

我愿意打赌问题是你的if语句:

if (animalName.options.value == "Other"){

如果您尝试获取所选选项的值,则应尝试更类似的内容:

if (animalName.options[animalName.selectedIndex].value == "Other"){

您尝试获取选项值是错误的。 它应该是:

if( this.options[this.selectedIndex].value == "Other") {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM