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

时间:2013-04-11 18:42:51

标签: javascript

好吧,这确实是一个非常奇怪的问题,但它真的发生在我身上,我写了一个在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。

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:0)

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

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