JavaScript關(guān)于select的相關(guān)操作說明
更新時間:2010年01月13日 03:43:44 作者:
JavaScript關(guān)于select的相關(guān)操作參考代碼。
一、 插入option
1、DOM方法
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);
2、new Option方法
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));
二、 清空option
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //清空Select里面的options
三、 設(shè)置默認(rèn)選中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默認(rèn)選中第二項
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延遲是為了防止dom渲染問題
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比較推薦的用setAttribute來設(shè)置
1、DOM方法
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);
2、new Option方法
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));
二、 清空option
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //清空Select里面的options
三、 設(shè)置默認(rèn)選中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默認(rèn)選中第二項
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延遲是為了防止dom渲染問題
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比較推薦的用setAttribute來設(shè)置
您可能感興趣的文章:
相關(guān)文章
(僅IE下有效)關(guān)于checkbox 三態(tài)
(僅IE下有效)關(guān)于checkbox 三態(tài)...2007-05-05
checkbox 多選框 聯(lián)動實現(xiàn)代碼
對于checkbox 多選的聯(lián)動效果,是個不錯的思路,附代碼2008-10-10
用 Javascript 驗證表單(form)中的單選(radio)值
在用 Javascript 驗證表單(form)中的單選框(radio)是否選中時,很多新手都會遇到問題,原因是 radio 和普通的文本框在獲取值的時候有很大不同.2009-09-09
在修改準(zhǔn)備發(fā)的批量美化select+可修改select時,在非IE下發(fā)現(xiàn)了幾個問題
在修改準(zhǔn)備發(fā)的批量美化select+可修改select時,在非IE下發(fā)現(xiàn)了幾個問題...2007-01-01

