按DOM方式

//使用dom的方法
$("floatingFrequencyCode").options.insertBefore(createOption(thestr,"01"),
$("floatingFrequencyCode").options[0]);
//则需要此方式来生成optino
functioncreateOption(thetext,thevalue)
...{
vartheoption=document.createElement("OPTION");
theoption.innerHTML=thetext;
theoption.value=thevalue;
returntheoption;
}
不按DOM方式
functioncreateOption(thetext,thevalue)
...{
vartheoption=document.createElement("OPTION");
theoption.text=thetext;
theoption.value=thevalue;
returntheoption;
}
$("floatingFrequencyCode").options.add(createOption(thestr,"01"),0);
//0是插入的位置,如果不写该参数,则直接追加在select尾部
本文介绍了两种在JavaScript中向<select>元素插入<option>的方法:一种遵循DOM标准,通过创建并插入新的DOM节点;另一种为简化版,适用于不严格遵循DOM规范的场景。文中提供了具体的函数实现与使用示例。

被折叠的 条评论
为什么被折叠?



