天天看點

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等其他屬性來擷取。

繼續閱讀