2010-05-03 2 views
0

다음 코드는 특정 id를 통해 하나의 selectOneMenu를 재설정합니다. 더 많은 selectOneMenus가 최상위 값으로 재설정되도록 동적으로 만드는 방법.reset selectOneMenu javascript

var test= document.getElementById('form1:text3'); 
test.options.selectedIndex=0; 

이 기능은 메뉴의 최상위 값으로 재설정되지만 동적으로 만드는 방법입니다.

도움을 주시면 감사하겠습니다.

+0

Dynamic? sth like ..i는 모든 selectonemenu를 반복하고 기본값으로 설정합니다. .. JSF를 사용하고 있습니다. – user234194

답변

0

다른 사람이이 문제를 언급 할 것이라고 확신하지만 ... jQuery을 사용해 보셨습니까?

jQuery는 DOM 요소를 쉽게 선택합니다. (그리고 그것은 쉽게 그들도 조작 할 수 있습니다!) 그래서

+0

답장을 thnks하지만 Jquery를 사용하고 있지 않습니다. 난 단지 모든 selectone 메뉴와 setto default를 반복하고 싶다. – user234194

0

을이 마크 업의 경우 :이 코드는

<select id="text3"> 
    <option>Original Item</option> 
</select> 

: 자바 스크립트를 사용하는 경우

window.onload = function() { 
    var test= document.getElementById('text3'); 
    // this will add 3 option tags as children of the select: 
    test.options[test.options.length] = new Option('TextValue','ValueValue'); 
    test.options[test.options.length] = new Option('TextValue2','ValueValue2'); 
    test.options[test.options.length] = new Option('TextValue3','ValueValue3'); 
    // this will select the 2nd option 
    test.options.selectedIndex=1; 
} 

, 다음 변환 될 것입니다 입력란 :

<select id="text3"> 
    <option>Original Item</option> 
    <option value="ValueValue">TextValue</option> 
    <option value="ValueValue2">TextValue2</option> 
    <option value="ValueValue3">TextValue3</option> 
</select> 

라이브러리를 사용하면 훨씬 쉽게 달성 할 수 있지만 시작하면됩니다.

마찬가지로 test.options.selectedIndex=1;을 사용하면 어느 것을 선택해야하는지 코드를 설정할 수 있습니다. 그것들은 인덱스가 0이므로 첫 번째 것은 0이고 두 번째는 1, 세번째는 2 등입니다.

+0

나는 모든 selectonemenu를 반복하고 모든 것을 기본값으로 설정하려고합니다. 나는 JSF를 사용하고있다. – user234194

관련 문제