<!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; } //清除指定的option function removeOne(s_id, index) { var obj = document.getElementById(s_id); obj.options.remove(index); } //清除选中的option function 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>
时间: 2024-10-09 16:59:37