2008-12-31 2 views
1

내 클라이언트가 JavaScript 클라이언트 측 프로그래머에게 옵션의 순서를 변경하여 페이지가로드 될 때 회사의 옵션이 선택 상자의 맨 위에 표시되도록 요청했습니다.Internet Explorer의 옵션 상자를 바꿀 수 있습니까?

다음 코드는 파이어 폭스에서 작동하지만 두 가지 옵션을 교환 할 때 Internet Explorer 7에서 실패

shippingLocation.options[0] = swap; 
: 나는 바닥에서 세 번째 줄에 다음에 오류가

function load{ 
     var shippingLocation = document.getElementById("location"); 
     var swap = null; 

     var initiallyFirstItem = shippingLocation.options[0].cloneNode(true); 

     var lastPos = null; 

     for(var i = 0; i < shippingLocation.length; i++) 
     { 
      if(shippingLocation.options[i].value == "XETEX") 
      { 
       swap = shippingLocation.options[i]; 
       lastPos = i; 
       break; 
      } 
     } 
     console.debug("sl: " + shippingLocation.options[0]); 
     console.debug("s: " + swap); 
     shippingLocation.options[0] = swap; 
     shippingLocation.options[lastPos] = initiallyFirstItem; 
     shippingLocation.selectedIndex = 0; 
    } 

"개체가이 속성 또는 메서드를 지원하지 않습니다."라는 오류가 표시됩니다.

전환 옵션 항목이있는 Internet Explorer의 쇠고기는 무엇입니까?

답변

1

글쎄, 나는 몇 가지 테스트를했는데 IE는 옵션 배열을 불변으로 간주합니다. 옵션을 편집 할 수는 있지만 옵션을 제거 할 수는 있지만 옵션을 다른 옵션으로 설정할 수는 없습니다.

그래서 당신은이 작업을 수행 할 때

shippingLocation.options[0] = swap; 

다른 하나의 옵션을 설정하려고하고 있기 때문에 IE가 불평한다.

내가 대신 이런 짓을 했을까 :

> Removed due to excessive stupidity on 
> my part. :P 

여기에 더 나은 방법 :

<script type="text/javascript"> 
function swap(obj,i,j) { 
    sib=obj.options[i].nextSibling; 
    obj.insertBefore(obj.options[i],obj.options[j]); 
    obj.insertBefore(obj.options[j],sib); 
} 
</script> 
+0

내가 다행 도움을 줄 수 =] – Salty

1

아 여기 있습니다. 위의 코드보다 훨씬 더 우아합니다.

function swapOptions(obj,i,j){ 
    var o = obj.options; 
    var i_selected = o[i].selected; 
    var j_selected = o[j].selected; 
    var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected); 
     var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected); 
    o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected; 
} 
관련 문제