JavaScript關(guān)于select的相關(guān)操作說明
更新時(shí)間: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)選中第二項(xiàng)
//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)選中第二項(xiàng)
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延遲是為了防止dom渲染問題
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比較推薦的用setAttribute來設(shè)置
您可能感興趣的文章:
- javascript操作select元素實(shí)例分析
- JavaScript通過select動(dòng)態(tài)更換圖片的方法
- javascript實(shí)現(xiàn)無限級(jí)select聯(lián)動(dòng)菜單
- javascript對(duì)下拉列表框(select)的操作實(shí)例講解
- javascript對(duì)select標(biāo)簽的控制(option選項(xiàng)/select)
- javascript模擬select,jselect的方法實(shí)現(xiàn)
- javascript模擬select實(shí)現(xiàn)代碼
- javascript 操作select下拉列表框的一點(diǎn)小經(jīng)驗(yàn)
- javascript獲取select值的方法分析
相關(guān)文章
js textarea自動(dòng)增高并隱藏滾動(dòng)條
textarea自動(dòng)增高并隱藏滾動(dòng)條2009-12-12(僅IE下有效)關(guān)于checkbox 三態(tài)
(僅IE下有效)關(guān)于checkbox 三態(tài)...2007-05-05[IE&FireFox兼容]JS對(duì)select操作
[IE&FireFox兼容]JS對(duì)select操作...2007-01-01checkbox 多選框 聯(lián)動(dòng)實(shí)現(xiàn)代碼
對(duì)于checkbox 多選的聯(lián)動(dòng)效果,是個(gè)不錯(cuò)的思路,附代碼2008-10-10用 Javascript 驗(yàn)證表單(form)中的單選(radio)值
在用 Javascript 驗(yàn)證表單(form)中的單選框(radio)是否選中時(shí),很多新手都會(huì)遇到問題,原因是 radio 和普通的文本框在獲取值的時(shí)候有很大不同.2009-09-09在修改準(zhǔn)備發(fā)的批量美化select+可修改select時(shí),在非IE下發(fā)現(xiàn)了幾個(gè)問題
在修改準(zhǔn)備發(fā)的批量美化select+可修改select時(shí),在非IE下發(fā)現(xiàn)了幾個(gè)問題...2007-01-01