2010-05-03 45 views
0

以下代碼通過特定的id重置一個selectOneMenu。如何使它動態更多selectOneMenus以最高值重置。reset selectOneMenu javascript

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

這將重置爲菜單的最大值,但是如何使其變爲動態。

任何幫助表示讚賞。

+0

動態?---某事像..我可以遍歷所有selectonemenu並設置爲默認值.. .. 我正在使用JSF。 – user234194 2010-05-04 05:37:18

回答

0

我確定別人會提到這個......但你有沒有試過jQuery

jQuery使選擇DOM元素變得容易。 (它可以很容易地操縱它們呢!)

+0

thnks的答覆,但我不使用jquery。我只是想遍歷所有選擇菜單並將其設置爲默認值。 – user234194 2010-05-04 05:39:58

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; 
} 

如果啓用JavaScript,然後將被轉化到:

<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,第二個是1,第三個是2,等等。

+0

我只是想遍歷所有我選擇的selectonemenu,並將其全部設置爲默認值。我正在使用JSF – user234194 2010-05-04 05:38:45