码迷,mamicode.com
首页 > 编程语言 > 详细

Select的创建,修改,取值,删除javascript代码

时间:2014-09-25 19:47:17      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   java   ar   sp   div   

<!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>

 

Select的创建,修改,取值,删除javascript代码

标签:style   blog   http   color   io   java   ar   sp   div   

原文地址:http://www.cnblogs.com/bigdesign/p/3993046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!