2013-04-11 116 views
0

好吧,那確實是一個很奇怪的問題,但它真的發生在我身上,我寫了一個JS,它在IE上工作,但它沒有火狐或鉻。在IE上工作的JS,不能在Chrome和FF上工作

我希望如果有人能與爲什麼發生這種情況賜教...

我的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上運行?...

當您選擇「其他」選項時從選擇標籤它應該設置div顯示屏蔽,這發生在IE上,但不是FF或鉻。

+1

你有沒有真的_invoke_' animalSelect'? – Zeta 2013-04-11 18:44:35

+0

你沒有真正解釋過什麼對你的代碼不起作用。另外,一些JS代碼可能在一個瀏覽器中工作,但不是另一個,這並不奇怪。它被稱爲[跨瀏覽器兼容性](https://www.google.com/search?q=cross+browser+compatibility)。 – Travesty3 2013-04-11 18:46:56

回答

2

我願意打賭,問題是你的if語句:

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

如果你正在試圖獲得所選選項的值,你應該嘗試更類似的東西:

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

對不起,比你快8秒;)無論如何都要+1。 – 2013-04-11 18:52:29

+0

真棒,它現在的作品!謝謝! 你介意解釋發生了什麼? @ _ @ – 2013-04-11 18:56:55

+0

OKes,我現在明白了,想通了;) – 2013-04-11 19:02:21

0

您嘗試獲取選項值是錯誤的。它應該是:

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

他只是在問題中加了這個,但是用他調用函數的方式(而不是事件監聽器),你將無法使用'this'。 – Travesty3 2013-04-11 18:54:26

+0

非常感謝:D – 2013-04-11 18:58:41

相關問題