1.动态创建select
function createSelect(){
var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
2.添加选项option
function addOption(){
//根据id查找对象,
var obj=document.getElementById('mySelect');
//添加一个选项
obj.add(new Option("文本","值")); //这个只能在IE中有效
obj.options.add(new Option("text","value")); //这个兼容IE与firefox
}
3.删除所有选项option
function removeAll(){
var obj=document.getElementById('mySelect');
obj.options.length=0;
}
4.删除一个选项option
function removeOne(){
var obj=document.getElementById('mySelect');
//index,要删除选项的序号,这里取当前选中选项的序号
var index=obj.selectedIndex;
obj.options.remove(index);
}
5.获得选项option的值
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].value;
6.获得选项option的文本
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].text;
7.修改选项option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index]=new Option("新文本","新值");
8.删除select
function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect.parentNode.removeChild(mySelect);
}
分享到:
相关推荐
js 操作select和optionjs 操作select和option
Javascript操作Select和Option 一个网页 挺全的
本人写的一个html页面,用js实现从数组读数据自动生成select控件
简单的例子,点击按钮就删除掉option,也可以通过jquery加载的时候进行删除。
javascript 获取select ->option中id、value、label属性及<option></option>中内容
select option 自定义图片的js 、css 以及代码展示,javaScript ;
1 检测是否有选中 2.动态创建select 3.添加选项option 4.删除所有选项option 5.删除一个选项option 6.获得选项option的值......
jquery获得select option值
1.动态创建select 代码如下: function createSelect(){ var mySelect = document.createElement(“select”); mySelect.id = “mySelect”; document.body.appendChild(mySelect); } 2.添加选项option 代码如下: ...
用js操作select的option,可以增加删除和判断value值是否在下拉列表存在
主要介绍了JS实现select选中option触发事件操作,结合实例形式总结分析了javascript针对select下拉选中option项触发事件相关操作技巧,需要的朋友可以参考下
主要介绍了JS更改select内option属性的方法,涉及JavaScript动态操作页面select元素属性的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
两个下拉列表,用JS动态添加Select中的Option元素值
<option value=”1″>one</option> <option value=”2″>two</option> <option value=”3″>three</option> </select> 则可通过以下script代码s来获取选中的value和text 代码如下: $(“#mySelect”).val(); /...
这个文档是本人在开发学习中下的别人的资料,觉得汇总的很好,后面在工作过程中修改了一些,非常有用,尤其是前端的,基本上对select option的操作都在这里面了。有收藏价值。
js 操作select 下拉框js 操作select 下拉框
js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全
Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等
复制代码代码如下: document.getElementById(“selectid”).options.length = 0; 方法二 复制代码代码如下: document.formName.selectName.options.length = 0; 方法三 复制代码代码如下: document....