Search

4/23/2008

select

JavaScript - Dynamic options
sel = $('selid');
sel.length // get the option length
sel[0] // get the first option reference
sel[0].innerHTML // get the innerHTML of the first option
sel[5].selected = true // select the 6th option

sel.options // collection of select options
sel.options.length // same as sel.length
sel.options[0].innerHTML // get the innerHTML of the first option
sel.options[0] = null // remove the first option from select

ie下面要動態新增options用innerHTML是不能work的
sel.innerHTML = '' // not work under ie6
可以用 sel.appendChild( document.createElement('option') );
或者 sel.options[sel.options.length] = new Option('foo', 'foovalue'); // works both under ff/ie

沒有留言: