Javascript操作select控件代碼實(shí)例
這篇文章主要介紹了Javascript操作select控件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
新增、修改、刪除、選中、清空、判斷存在等
1、判斷select選項(xiàng)中 是否存在Value=”paraValue”的Item
function jsselectisexititem(objselect,objitemvalue)
{
var isexit = false;
for(var i=0;i<objselect.options.length;i++)
{
if(objselect.options[i].value == objitemvalue)
{
isexit = true;
break;
}
}
return isexit;
}
2、向select選項(xiàng)中加入一個(gè)Item
function jsadditemtoselect(objselect,objitemtext,objitemvalue)
{
//判斷是否存在
if(jsselectisexititem(objselect,objitemvalue))
{
alert("該item的value值已經(jīng)存在");
}
else
{
var varitem = new option(objitemtext,objitemvalue);
// objselect.options[objselect.options.length] = varitem;
objselect.options.add(varitem);
alert("成功加入");
}
}
3、從select選項(xiàng)中刪除一個(gè)Item
function jsremoveitemfromselect(objselect,objitemvalue)
{
//判斷是否存在
if(jsselectisexititem(objselect,objitemvalue))
{
for(var i=0;i<objselect.options.length;i++)
{
if(objselect.options[i].value == objitemvalue)
{
objselect.options.remove(i);
break;
}
}
alert("成功刪除");
}
else
{
alert("該select中 不存在該項(xiàng)");
}
}
4、刪除select中選中的項(xiàng)
function jsRemoveSelectedItemFromSelect(objSelect){
var length = objSelect.options.length - 1;
for(var i = length; i >= 0; i—){
if(objSelect[i].selected == true){
objSelect.options[i] = null;
}
}
}
5、修改select選項(xiàng)中 value=”paraValue”的text為”paraText”
function jsupdateitemtoselect(objselect,objitemtext,objitemvalue)
{
//判斷是否存在
if(jsselectisexititem(objselect,objitemvalue))
{
for(var i=0;i<objselect.options.length;i++)
{
if(objselect.options[i].value == objitemvalue)
{
objselect.options[i].text = objitemtext;
break;
}
}
alert("成功修改");
}
else
{
alert("該select中 不存在該項(xiàng)");
}
}
6、設(shè)置select中text=”paraText”的第一個(gè)Item為選中
function jsselectitembyvalue(objselect,objitemtext)
{
//判斷是否存在
var isexit = false;
for(var i=0;i<objselect.options.length;i++)
{
if(objselect.options[i].text == objitemtext)
{
objselect.options[i].selected = true;
isexit = true;
break;
}
}
//show出結(jié)果
if(isexit)
{
alert("成功選中");
}
else
{
alert("該select中 不存在該項(xiàng)");
}
}
7、設(shè)置select中value=”paraValue”的Item為選中
document.all.objSelect.value = objItemValue;
8、得到select的當(dāng)前選中項(xiàng)的value
var currSelectValue = document.all.objSelect.value;
9、得到select的當(dāng)前選中項(xiàng)的text
var currselecttext = document.all.objselect.options[document.all.objselect.selectedindex].text;
10、得到select的當(dāng)前選中項(xiàng)的Index
var currSelectIndex = document.all.objSelect.selectedIndex;
11、清空select的項(xiàng)
document.all.objSelect.options.length =0;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javascript獲取select值的方法完整實(shí)例
- 詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選
- angularJs select綁定的model取不到值的解決方法
- js動(dòng)態(tài)設(shè)置select下拉菜單的默認(rèn)選中項(xiàng)實(shí)例
- JS實(shí)現(xiàn)select選中option觸發(fā)事件操作示例
- Vue.js做select下拉列表的實(shí)例(ul-li標(biāo)簽仿select標(biāo)簽)
- vue.js 獲取select中的value實(shí)例
- JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法
相關(guān)文章
css3元素簡(jiǎn)單的閃爍效果實(shí)現(xiàn)(html5 jquery)
本篇文章主要介紹了css3元素簡(jiǎn)單的閃爍效果實(shí)現(xiàn)(html5 jquery) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
微信小程序防止多次點(diǎn)擊跳轉(zhuǎn)和防止表單組件輸入內(nèi)容多次驗(yàn)證功能(函數(shù)防抖)
這篇文章主要介紹了微信小程序防止多次點(diǎn)擊跳轉(zhuǎn)和防止表單組件輸入內(nèi)容多次驗(yàn)證功能(函數(shù)防抖),需要的朋友可以參考下2019-09-09
談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記9)
這篇文章主要介紹了談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記9)的相關(guān)資料,需要的朋友可以參考下2015-12-12
線路分流自動(dòng)智能跳轉(zhuǎn)代碼,自動(dòng)選擇最快鏡像網(wǎng)站(js)
線路分流自動(dòng)智能跳轉(zhuǎn)代碼,自動(dòng)選擇最快鏡像網(wǎng)站,自動(dòng)選擇電信或網(wǎng)通線路跳轉(zhuǎn)代碼2011-10-10
基于canvasJS在PHP中制作動(dòng)態(tài)圖表
這篇文章主要介紹了基于canvasJS在PHP中制作動(dòng)態(tài)圖表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

