- 动态创建select
function createSelect() {
var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
2.添加选项option
function addOption() {
var s = document.getElementById("mySelect");
s.add(new Option("text", "value"))//IE
s.options.add(new Option("text", "value"));firefox
}
3.删除所有optiong和指定的option
function remove() {
var obj = document.getElementById("mySelect);
obj.options.length = 0;
//删除指定
var index = obj.selectedIndex;
ob.optionsj.remove(index);
}
4.获取option的文本和值
function val() {
var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
var text = obj.optiosn[index].text;
var value = ojb.options[index].value;
}
注意,可以使用obj.selectedIndex != -1来判断选项是否被选中;