2009-08-26 61 views
0

我有下面的代碼 - 它是用來當用戶點擊下拉只加載默認選項加載一個下拉,因爲這個下拉是相當大的,IE瀏覽器沒有按」非常喜歡這個。下面是代碼:IE 6的JavaScript替代選擇下拉問題

function populateDropDown(id, code) { 
    var currentSelect = document.getElementById(id); 
    <%--Don't enable the dropdown if it has more than one entry already - assume this to be populated.--%> 
    if(currentSelect.length == 1) { 
      currentSelect.remove(0); 
      var selectedIndex = 0; 
      for(var index = 0; index < codes.length; index++) { 
        var newOption = document.createElement('option'); 
        newOption.value = codes[index]; 
        newOption.text = values[index]; 
        try { 
          currentSelect.add(newOption, null); // standards compliant 
        } 
        catch(ex) 
        { 
          currentSelect.add(newOption); // IE only 
        } 
        if(codes[index] == code) { 
          selectedIndex = index; 
        } 
      } 
      currentSelect.selectedIndex = selectedIndex; 
    } 
} 

此代碼的工作在Opera 9.x中,IE 7 - 但不是IE 6(我測試在Opera,因爲我喜歡Opera蜻蜓 - 但它真的只在IE 7的工作和6)。

在IE 6,將碼確實填充下拉,但它設置所選的值,以在下拉列表,而不是所選擇的值的第一個值。在所提及的另外兩個瀏覽器中,所選值被設置爲適當的值。

我沒有使用Javascript大師以任何方式 - 所以,如果有人知道爲什麼IE 6是這樣做的,怎麼去解決它,那會被讚賞。 另請注意,該評論有一個JSP註釋 - 在將此Javascript發送到瀏覽器之前將其刪除(這不是無效註釋)。

+0

來自哪裏的,如果(代碼[指數] ==代碼)代碼? – 2009-08-26 22:14:11

+0

對不起,Russ,忘了添加簽名。簽名是functionName(id,code)。代碼來自最初選定的值,通過JSP EL函數(因此爲什麼檢查不填充已經填充的下拉列表 - 如果該下拉列表中沒有兩個以上的選項,則存在*更大的*問題需要擔心!) – MetroidFan2002 2009-08-26 23:47:16

+0

編輯添加簽名。 – MetroidFan2002 2009-08-26 23:52:07

回答

1

我以前碰到這個確切的問題。如果嘗試在將焦點返回到文檔之前訪問select元素(選項)的動態創建的子元素,則設置selectedIndex將失敗並將默認爲第一個項目。

我會後回來時,我能找到我發現,在修復的文章。敬請期待!

UPDATE:

找到了!

相反,<select>元素上設置selectedIndex的,找到你想要的<option>元素,並將其「選擇」屬性設置爲true:

var currentSelect = document.getElementById(id); 
if(currentSelect.length == 1) { 
     currentSelect.remove(0); 
     var selectedIndex = 0; 
     for(var index = 0; index < codes.length; index++) { 
       var newOption = document.createElement('option'); 
       newOption.value = codes[index]; 
       newOption.text = values[index]; 
       try { 
         currentSelect.add(newOption, null); // standards compliant 
       } 
       catch(ex) 
       { 
         currentSelect.add(newOption); // IE only 
       } 
       if(codes[index] == code) { 
         selectedIndex = index; 
       } 
     } 
     // currentSelect.selectedIndex = selectedIndex; 
     // Try this: 
     currentSelect.options[selectedIndex].setAttribute('selected', true); 
} 
+0

嘿科裏 - 這很有趣。我會盡快嘗試它,如果它有效(我希望它可以),我會接受你的答案。在此之前,你必須做出正確的決定:) – MetroidFan2002 2009-08-26 23:48:44

+0

沒問題。我遇到了同樣的問題,但使用jQuery。在IE7,IE8和FF2 +中一切都按預期工作,但在IE6中失敗。這裏是我發現修復的實際文章:http://brh.numbera.com/experiments/browserdemos/ie6-adding-options.html – 2009-08-27 03:04:08

+0

我接受你的答案,但我添加了我自己的 - 你的答案幫助我。 – MetroidFan2002 2009-08-27 14:28:59

0

Cory的意見幫了我,但他的代碼沒有產生結果我希望IE 6以下。但他指出,重點可能是問題。下面的代碼片段使得它可以在IE6下正確選擇 - 它看起來很奇怪,因爲它加載下拉菜單然後選擇它,但是功能是我想要的,並且比加載靜態HTML更快。

currentSelect.focus(); 
currentSelect.selectedIndex = selectedIndex; 

關注的輸入,然後設置選定的索引,在IE 6 - 雖然下拉跳轉到第一輸入非常快,然後跳回。但是,只要它的作品...

+0

也許嘗試這一個:而不是做'currentSelect。focus();'在設置'selectedIndex'之前,嘗試超時設置它:'setTimeout(function(){currentSelect.selectedIndex = selectedIndex;},0);' – 2009-08-27 15:29:53