JavaScript操作select元素和option的實例代碼
更新時間:2016年01月29日 14:05:42 作者:歐歐歐鋒_
這篇文章主要介紹了JavaScript操作select元素和option的實例代碼的相關(guān)資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼,具體代碼如下所示:
<!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://www.w.org/TR/xhtml/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w.org//xhtml">
<head>
<title></title>
<!--添加jquery-->
<script src="../Script/jQuery/jquery-...min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
createSelect("select", "addSel");
addOption("addSel", "first", "第一個數(shù)據(jù)");
addOption("addSel", "secord", "第二個數(shù)據(jù)");
addOption("addSel", "three", "第三個數(shù)據(jù)");
addOption("addSel", "four", "第四個數(shù)據(jù)");
addOption("addSel", "fives", "第五個數(shù)據(jù)");
removeOneByIndex("addSel", );
removeOneByObj("addSel", "secord");
//添加一個option更改事件 調(diào)用自己寫的方法
$("#addSel").change(function () {
alert("舊文本:"+getOptionText("addSel") + "舊Value:" + getOptionValue("addSel"));
editOptions("addSel", "新文本","新Value"); //注意:不傳value值的時候 value值默認(rèn)為text的值
alert("新文本:" + getOptionText("addSel") + "新Value:" + getOptionValue("addSel"));
})
})
//動態(tài)創(chuàng)建帶id的元素
function createSelect(element, id) {
var select = document.createElement(element);
select.id = id;
document.body.appendChild(select);
}
//根據(jù)select的id 添加選項option
function addOption(selectID,value,text) {
//根據(jù)id查找對象,
var obj = document.getElementById(selectID);
obj.options.add(new Option(text, value)); //這個兼容IE與firefox
}
//刪除所有選項option
function removeAll(selectID) {
var obj = document.getElementById(selectID);
obj.options.length = ;
}
//根據(jù) index 值刪除一個選項option
function removeOneByIndex(selectID,index) {
var obj = document.getElementById(selectID);
//index,要刪除選項的序號,這里取當(dāng)前選中選項的序號
//var index = obj.selectedIndex;//獲取選中的選項的index;
obj.options.remove(index);
}
//根據(jù) value或者text值刪除一個選項option
function removeOneByObj(selectID, textOrValue) {
var obj = document.getElementById(selectID);
//index,要刪除選項的序號,這里取當(dāng)前選中選項的序號
//var index = obj.selectedIndex;//獲取選中的選項的index;
for (var i = ; i < obj.options.length; i++) {
if (obj.options[i].innerHTML == textOrValue || obj.options[i].value == textOrValue) {
obj.options.remove(i);
break;
}
}
}
//獲得一個Option Value;
function getOptionValue(selectID){
var obj = document.getElementById(selectID);
var index=obj.selectedIndex; //序號,取當(dāng)前選中選項的序號
var val = obj.options[index].value;
return val;
}
//獲得一個option Text;
function getOptionText(selectID) {
var obj = document.getElementById(selectID);
var index=obj.selectedIndex; //序號,取當(dāng)前選中選項的序號
var val = obj.options[index].text;
return val;
}
//修改選中的option
function editOptions(selectID,newText,newValue) {
var obj = document.getElementById(selectID);
var index=obj.selectedIndex; //序號,取當(dāng)前選中選項的序號
obj.options[index] = new Option(newText, newValue);
obj.options[index].selected = true;
}
//刪除select
function removeSelect(){
var select = document.getElementById("select");
select.parentNode.removeChild(select);
}
</script>
</head>
<body>
</body>
</html>
以上所述是小編給大家分享的JavaScript操作select元素和option的實例代碼,希望對大家有所幫助。
您可能感興趣的文章:
- javascript操作select元素實例分析
- 獲取select元素被選中的文本內(nèi)容的js代碼
- js querySelector和getElementById通過id獲取元素的區(qū)別
- js獲取當(dāng)前select 元素值的代碼
- js 獲取當(dāng)前select元素值的代碼
- JavaScript Select和Option列表元素上下左右移動
- extjs 列表框(multiselect)的動態(tài)添加列表項的方法
- JS對HTML標(biāo)簽select的獲取、添加、刪除操作
- 使用js對select動態(tài)添加和刪除OPTION示例代碼
- JavaScript實現(xiàn)將數(shù)組數(shù)據(jù)添加到Select下拉框的方法
- JS動態(tài)添加與刪除select中的Option對象(示例代碼)
- JS & JQuery 動態(tài)添加 select option
- JavaScript實現(xiàn)向select下拉框中添加和刪除元素的方法
相關(guān)文章
Js使用WScript.Shell對象執(zhí)行.bat文件和cmd命令
這篇文章主要介紹了Js使用WScript.Shell對象執(zhí)行.bat文件和cmd命令,需要的朋友可以參考下2014-12-12
Three.js實現(xiàn)瀏覽器變動時進(jìn)行自適應(yīng)的方法
這篇文章主要給大家介紹了關(guān)于Three.js實現(xiàn)瀏覽器變動時進(jìn)行自適應(yīng)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
webpack-dev-server搭建本地服務(wù)器的實現(xiàn)
當(dāng)我們使用webpack打包時,發(fā)現(xiàn)每次更新了一點代碼,都需要重新打包,我們希望本地能搭建一個服務(wù)器,本文就介紹如何使用webpack-dev-server搭建本地服務(wù)器,感興趣的可以了解一下2021-07-07
詳解webpack+ES6+Sass搭建多頁面應(yīng)用
這篇文章主要介紹了webpack+ES6+Sass搭建多頁面應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

