select options常用操作
1.判断select选项中 是否存在Value=
"value"
的option元素
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选项中 加入一个Item
function addOptions(objsel,text,value){
if (valueExit(objsel,value)) {
alert("改属性值已经存在");
};
var option = new Option(text,value);
objsel.add(option,null);
alert('添加成功');
}
3.从select选项中 删除一个Item
function removeOpt(objSelect,objItemValue)
{
//判断是否存在
if(valueExit(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中 不存在该项");
}
}
4.修改select选项中 value=
"paraValue"
的text为
"paraText"
<script type="text/javascript">
var select = document.getElementById('sele');
function chaTeByval(objSelect,objItemText,objItemValue)
{
//判断是否存在
if(isExit(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中 不存在该项");
}
} chaTeByval(select,'美国',6);
5.设置select中text=
"paraText"
的第一个Item为选中
<script type="text/javascript">
var select = document.getElementById('sele');
function selbyText(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;
}
}
if(isExit)
{
alert("成功选中"); }
else
{
alert("该select中 不存在该项");
}
} selbyText(select,'上海');
</script>
6.设置select中value=
"paraValue"
的Item为选中
<script type="text/javascript">
var select = document.getElementById('sele');
function selbyText(objSelect,objItemValue)
{
//判断是否存在
var isExit = false;
for(var i=0;i<objSelect.options.length;i++)
{
if(objSelect.options[i].value == objItemValue)
{
objSelect.options[i].selected = true;
isExit = true;
break;
}
}
if(isExit)
{
alert("成功选中"); }
else
{
alert("该select中 不存在该项");
}
} selbyText(select,1);
</script>
7.得到select的当前选中项的value select.options[select.options.selectedIndex].value
8.得到select的当前选中项的text select.options[select.options.selectedIndex].text
9.得到select的当前选中项的Index select.options.selectedIndex;
10.清空select的项 select.options.length=0 select.innerHTML="";
最新文章
- 玩转UICollectionViewLayout
- .Net Core下如何管理配置文件
- 渲染物体到一张UITexture上
- 零件分组_DP
- Fatal error: Call to a member function bind_param() on a non-object in
- 【CSLA】Component-based,Scalable,LogicalArchitecture
- 使用django+mysql+scrapy制作的一个小说网站
- org/hamcrest/SelfDescribing
- Train Problem II(卡特兰数+大数乘除)
- 读改善c#代码157个建议:建议7~9
- Java基础学习
- 【Android Developers Training】 17. 停止和重启一个Activity
- lesson - 3 ls /cd /path /alias /快捷键
- Saltstack的安装和配置
- JSP常见的三个编译指令
- linux /proc/sys/vm/中各个文件含义
- python学习笔记3-列表
- VirtualBox导致Vmware的虚拟机桥接模式网络不通
- 开发一个简单的postgresql extension
- VCL界面控件DevExpress VCL发布v18.2.2|附下载
热门文章
- Remove Duplicates from Sorted Array II ——LeetCode
- Break、continue、return用法(C++)
- CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站
- python BDD&;TDD
- mysql group by的用法 注意
- 漫步支持向量机(svm)之一
- ios 打电话结束返回到应用中
- sparkSQL1.1入门之二:sparkSQL执行架构
- IOS Dictionary和Model相互转换
- Ubuntu设置环境变量的几种方法