天天看点

select中option解析

select动态添option:

var sel=document.getElementById(selectId);         //获取select

var newOpt=document.createElement("OPTON");

newOpt.setAttribute("value",optValue);            //option的value值

newOpt.innerHTML=optText;                    //option中文本值

sel.appendChild(newOpt);                     //将option加入到sel中

获得指定位置的option

var sel=document.getElementById(selectId);

var index=position;                        //option中未知索引,0,1,2,,

var theOpt=sel.options[index];                 //获得该option

或者用jquery

var theOpt=jQuery("#selectId option[index='position']");

表示获取index为position的option

var theOpt=jQuery("#selectId option[value='value']");

表示根据value值来获取指定的option

注意:动态添加option的select不能通过index来获取option,

可以用value等其他属性来获取。

继续阅读