<select>タグ中の<option>タグの数は、'select'エレメントの'length'属性に格納されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!doctype html> < html > < head > < meta charset = "UTF-8" > < script type = "text/javascript" language = "javascript" > <!-- function OnLoad() { // selectエレメント取得 var elmSelect = document.getElementById( "id_select" ); // optionタグの数を取得 var length = elmSelect.length; // lengthを表示 document.getElementById( "id_result" ).innerHTML = "select.length = " + length + "<br/>"; } // --> </ script > </ head > < body onload = "OnLoad()" > < h1 ><select>タグの中の<option>タグの数を取得する</ h1 > < select id = "id_select" > < option >アイテム1</ option > < option >アイテム2</ option > < option >アイテム3</ option > < option >アイテム4</ option > < option >アイテム5</ option > </ select > < h2 >実行結果</ h2 > < div id = "id_result" > </ div > </ body > </ html > |
select.length = 5