- 動態(tài)創(chuàng)建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來判斷選項是否被選中;