2012-10-19 28 views
0

在我的形式如何重置一個按鈕內的所有選擇框?

<div class="container"> 
    <select name="sel1"> 
     <option value="0">0</option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
    </select> 
    <select name="sel2"> 
     <option value="0">Zero</option> 
     <option value="1">One</option> 
     <option value="2">Two</option> 
    </select> 
</div> 
<input type="button" onclick="resetAll()" value="Reset"/> 

我需要復位兩個SEL1,SEL2爲默認值。即selectedIndex = 0;

function resetAll() 
{ 
    //what should i write here to do this 
}  

我有超過20個選擇框。任何幫助?

回答

0

只需一個ID添加到容器中的項目,這樣你就可以輕鬆地引用它。這樣,你只恢復駐留在它的選擇框,而不是任何其他的,你可以在頁面上:

function resetAll() { 
    var container = document.getElementById('container'); 
    var children = container.getElementsByTagName('select'); 
    for (var i = 0; i < children.length; i++) { 
     children[i].selectedIndex = 0; 
    } 
} 
-1

試試這個

var secondCombo = document.getElementsByName("sel1")[0]; 
secondCombo.value = 0; 
0

試試這個,這應該爲任何數量的工作drop downs

function resetAll() 
{ 
    var selects = document.getElementsByTagName('select'); 
    var len = selects.length; 
    for(var i=0; i<len; i++){ 
     selects[i].selectedIndex=0; 
    } 
} 

演示:http://jsfiddle.net/LAaRv/1/

UPDATE:

如果你想RESE T單獨「SEL1」和「SEL2」那就試試這個,

function resetAll() 
{ 
    var selects = document.getElementsByTagName('select'); 
    var len = selects.length; 
    for(var i=0; i<len; i++){ 
     if(selects[i].name == "sel1" || selects[i].name == "sel2"){ 
      selects[i].selectedIndex=0; 
     } 
    } 
} ​ 

+0

這會重置頁面上的所有**