2011-01-06 11 views

答えて

44
document.getElementById('id').options.length = 0; 

または

document.getElementById('id').innerHTML = ""; 
+1

+1と変わったフラッシュがあります。options.length = 0;' – epascarello

+0

はい、 'options.length'は1996年とNetscape 2のすべてに戻ります! – bobince

+0

良い古いオプション.length – FreshPro

5
var select = document.getElementById('yourSelectBox'); 

while (select.firstChild) { 
    select.removeChild(select.firstChild); 
} 
+0

IE6で 'document.getElementById( 'id')に –

0
<select id="thing"><option>fdsjl</option></select> 
<script> 
var el = document.getElementById('thing'); 
el.innerHTML = ''; 

// or this 

while (el.firstChild) { 
    el.removeChild(el.firstChild) 
} 
</script> 
+0

innerHTMLはブラウザ間で安全ですか? – hungryMind

+1

Yesssssssssssss –

0

ITSは非常に簡単で、JavaScriptとDOMを使用した:0にlength設定

while (selectBox.firstChild) 
    selectBox.removeChild(selectBox.firstChild); 
+0

imageDropdownBox.options.length – akshay

+0

はい - @hunterによると、options.length = 0はすべてのオプション(Javascript配列のようなアクセス)を解除するのに良い方法です。その答えを投票しました。しかし残念ながら、それはすべてのブラウザでうまく動作しません。古いブラウザが必要な場合は、薄いDOM-level-1が最適なAPIです。 – Guss

0

は、おそらく最良の方法ですが、あなたのことができこれを行う:

var mySelect = document.getElementById("select"); 
var len = mySelect.length; 
for (var i = 0; i < len; i++) { 
    mySelect.remove(0); 
} 
0

私は見つけることができた最速の溶液(このarticleから採取された)次のコードである。

// Fast javascript function to clear all the options in an HTML select element 
// Provide the id of the select element 
// References to the old <select> object will become invalidated! 
// This function returns a reference to the new select object. 
function ClearOptionsFast(id) 
{ 
    var selectObj = document.getElementById(id); 
    var selectParentNode = selectObj.parentNode; 
    var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy 
    selectParentNode.replaceChild(newSelectObj, selectObj); 
    return newSelectObj; 
} 
関連する問題