dropdownlist加Js 实现联动

1.在前天添加两个控件DropDownList和TextBox,在后台给DropDownList赋值和属性的值

View Code
            DropDownList1.Items.Add(new ListItem(" ", " "));
ListItem item
= new ListItem("1+1", "1");//绑定value
item.Attributes.Add("key", "2");//绑定属性的value
DropDownList1.Items.Add(item);
ListItem item2
= new ListItem("2+2", "2");
item2.Attributes.Add(
"key", "4");
DropDownList1.Items.Add(item2);

2.在前台

View Code
//DropDownList1后面添加onchange="ChangeDDL(this)"

<script type="text/javascript">
function ChangeDDL(obj) {
var DropDownList1
= document.getElementById("<%=DropDownList1.ClientID %>");
var TextBox1
= document.getElementById("<%=TextBox1.ClientID %>");
if (obj.value == " ") {
TextBox3.value
= "";
}
else {
var text
= obj.options[obj.selectedIndex].getAttribute("key");
TextBox1.value
= text ;
}
}
</script>
posted @ 2011-08-11 09:45  bule  阅读(1029)  评论(0编辑  收藏  举报