首页 > 代码库 > Select的创建,修改,取值,删除javascript代码
Select的创建,修改,取值,删除javascript代码
<!DOCTYPE html><html><head><title>test</title><meta http-equiv="content-type" content="text/html; charset=utf-8"/></head><body><script>function createSelect(s_id) { var mySelect = document.createElement("select"); mySelect.id = s_id; document.body.appendChild(mySelect);}function addOption(x_text, y_value, s_id) { var obj = document.getElementById(s_id); obj.add(new Option(x_text, y_value));}//清除select选项function removeAll(s_id) { var obj = document.getElementById(s_id); obj.options.length = 0;}//清除指定的optionfunction removeOne(s_id, index) { var obj = document.getElementById(s_id); obj.options.remove(index);}//清除选中的optionfunction removeSelectOne(s_id) { var obj = document.getElementById(s_id); var s_index = obj.selectIndex; obj.options.remove(s_index);}//获得选项option的value值 text值function getValue(s_id) { var obj = document.getElementById(s_id); var s_index = obj.selectedIndex; var s_value = obj.options[s_index].value; var s_text = obj.options[s_index].text; alert(s_value); alert(s_text);}function edit(s_id, index, x_text, y_value ) { var obj = document.getElementById(s_id); obj.options[index] = new Option("x_text", "y_value");}function removeSelect(s_id) { var mySelect = document.getElementById(s_id); mySelect.parentNode.removeChild(mySelect);}createSelect("mySelect");addOption("文本1", "值1", "mySelect");addOption("文本2", "值2", "mySelect");addOption("文本3", "值3", "mySelect");createSelect("mySelect2");addOption("文本1", "值1", "mySelect2");addOption("文本2", "值2", "mySelect2");addOption("文本3", "值3", "mySelect2");// removeAll("mySelect2");// removeOne("mySelect2",2);// removeSelectOne("mySelect");// getValue("mySelect");// edit("mySelect",2);// removeSelect("mySelect2")</script></body></html>
Select的创建,修改,取值,删除javascript代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。