js--select下拉框中的option对象
文章目录
创建Option对象
var option1 = document.createElement('option');
动态创建Option对象
function createSelect(){
var new_option= document.createElement("select");
new_option.id = "mySelect";
document.body.appendChild(new_option);
}
获得选项option的值
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].value;
获得选项option的文本
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].text;
修改选项option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index]=new Option("文本","值");
删除select
function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect.parentNode.removeChild(mySelect);
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论