2012-09-17 4 views
0

일부 값을 가진 콤보 상자가 있는데, 몇 가지 값이 있습니다. 콤보 상자에서 하나의 값을 선택하고 조작을 수행하고 선택한 값으로 돌아옵니다 (세션에서이 값들을 설정하십시오). 이제 콤보 상자에서 세션에있는 값을 자동으로 선택해야합니다.동적으로 생성 된 combobox에서 중복 값을 제거합니다.

나는 자바 스크립트 아래 따랐다하고 그것을 잘 작동하지만, 콤보 상자에 이미 중복 값을 생성합니다

function addCombo() { 
    var reportID = '<%=reportid%>'; 
    var reportName = '<%=reportname%>'; 

    var textb = document.getElementById("reportnames"); 

    var option = document.createElement("option"); 
    option.text = reportName; 
    option.value = reportID; 
    option.selected='selected'; 
    try { 
     textb.add(option, null); //Standard 
    }catch(error) { 
     textb.add(option); // IE only 
    } 
    textb.value = ""; 
} 

사람을 식별하는 데 도움이 수 있습니까? 그것은 나를 해결,

+0

이 링크는 도움이 될 수 있습니다. http://stackoverflow.com/questions/4590311/set-option-selected-attribute-from-dynamic-created-option – Anoop

답변

0

..

function hasValue(element, value) { 
    var results = true; 

    for (var i=element.options.length-1; i >= 0; i--) (function() { 

     if (element.options[i].value == value) (function() { 
      results = false; 
     }()); 
    }()); 

    return (results); 
}; 

쉽게 걸릴 난 내가 다음 코드를 추가하지 않고 값을 제거하지 않은 :

function addCombo() { 
    var reportID = 'aa'; 
    var reportName = 'aa'; 

    var textb = document.getElementById("reportnames"); 
    var option = document.createElement("option"); 

    if (hasValue(textb, reportID)) (function() { 


     // selected attr remove 
     for (var i=textb.options.length-1; i >= 0; i--) (function() { 

      if (textb.options[i].selected == true) (function() { 

       textb.options[i].removeAttribute("selected"); 
      }()); 
     }()); 

     option.text = reportName; 
     option.value = reportID; 

     // selected new option element 
     option.setAttribute("selected", "selected"); 


     try { 
      textb.add(option, null); //Standard 
     } catch(error) { 
      textb.add(option); // IE only 
     }; 

     /* 
     * I do not understand selected added option value "" ?? 
     */ 
     //textb.value = ""; 

     }()); 
}; 

hasValue 기능 :

0

확인 시도를 이해 문제.

function addCombo() { 
    var reportID = '<%=reportid%>'; 
    var options= document.getElementById('reportnames').options; 
    for (var i= 0, n= options.length; i < n ; i++) { 
     if (options[i].value==reportID) { 
      document.getElementById("reportnames").selectedIndex = i; 
      break; 
     } 
    } 
} 
관련 문제