select中option解析

来源:互联网 发布:计算机二级vb 编辑:程序博客网 时间:2024/05/20 18:54
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等其他属性来获取。
0 0