欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS對(duì)select控件option選項(xiàng)的增刪改查示例代碼

 更新時(shí)間:2013年10月21日 16:23:30   作者:  
Javascript操作select是表單中比較常見(jiàn)的,大家可以在網(wǎng)上搜索到很多的相關(guān)資料,接下來(lái)為大家詳細(xì)介紹下,JS動(dòng)態(tài)操作select中的各種方法,感興趣的朋友可以參考下
Javascript 操作select是表單中常見(jiàn)的一種,下面介紹幾種常用的JS動(dòng)態(tài)操作select中的各種方法:
復(fù)制代碼 代碼如下:

//動(dòng)態(tài)創(chuàng)建select
function createSelect()
{
var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}

復(fù)制代碼 代碼如下:

//添加選項(xiàng)option
function addOption()
{
//根據(jù)id查找對(duì)象,
var obj=document.getElementById('mySelect');
//添加一個(gè)選項(xiàng)
obj.add(new Option("文本","值")); //這個(gè)只能在IE中有效
obj.options.add(new Option("text","value")); //這個(gè)兼容IE與firefox
}

復(fù)制代碼 代碼如下:

//刪除所有選項(xiàng)option
function removeAll()
{
var obj=document.getElementById('mySelect');
obj.options.length=0;
}

復(fù)制代碼 代碼如下:

//刪除一個(gè)選項(xiàng)option
function removeOne()
{
var obj=document.getElementById('mySelect');
//index,要?jiǎng)h除選項(xiàng)的序號(hào),這里取當(dāng)前選中選項(xiàng)的序號(hào)
var index=obj.selectedIndex;
obj.options.remove(index);
}

復(fù)制代碼 代碼如下:

//獲得選項(xiàng)option的文本
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)
var val = obj.options[index].text;

復(fù)制代碼 代碼如下:

//修改選項(xiàng)option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)
var val = obj.options[index]=new Option("新文本","新值");

相關(guān)文章

最新評(píng)論