日期:2014-05-18  浏览次数:20476 次

请教: 能否用一段JS实现ListBox选中一项时,在TextBox中显示选中项的value ?
如题

------解决方案--------------------
应该是可以的,但你要在网页上查找 listbox的ID,然后JS中调用选择项的selectvalue,理论上可以,试试,但因为是服务器控件所以也不能保证。
------解决方案--------------------
完全可以,下面是已经实现的代码
<script language= "javascript " >
function Display()
{
var listBox = document.getElementById( "ListBox1 ");
alert(listBox.options[listBox.selectedIndex].value);
document.getElementById( "TextBox1 ").innerText = listBox.options[listBox.selectedIndex].value;
}
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:ListBox ID= "ListBox1 " runat= "server " onclick = "Display(); ">
<asp:ListItem Selected = "true " Value= "1 " > sfsfsfsf </asp:ListItem>
<asp:ListItem Value= "2 " > s34242 </asp:ListItem>
<asp:ListItem Value= "3 "> sdf2233 </asp:ListItem>
</asp:ListBox>
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox> </div>
</form>
</body>
</html>
------解决方案--------------------
应该能行...

但现在没有环境,踩个脚印
------解决方案--------------------
yumiaojin() 兄的方法可行,和ASP中是一样的。
------解决方案--------------------
接分,很简单,会JS差不多都懂
------解决方案--------------------
onclick = "Display(); "
最好把这里改成:
onclick = "Display(this); "

function Display(obj)
{
.....
}

------解决方案--------------------
很简单的阿,上面代码已经实现了
------解决方案--------------------
1楼正解,查看listBox控件生成的html代码,应该不复杂
------解决方案--------------------
混分!
这个都生成在页面上的东东了,JS都能找得到,没什么不行
------解决方案--------------------
嘿嘿,一楼正解。
------解决方案--------------------
一定能行
------解决方案--------------------
一楼的肯定不行

因为onclick 是服务端事件

需注册客户端事件
listBox1.Attributes.Add( "onclick ", "Display(); ");
------解决方案--------------------
而且还有错 如果selectedIndex 为-1 时就会报错



var list = document.getElementById( "list ");
var currIndex = list.selectedIndex;

if(currIndex > = 0){
document.getElementById( "文本框ID ").value = list.options[currIndex].value;
}
------解决方案--------------------
一楼的肯定不行

因为onclick 是服务端事件

需注册客户端事件
listBox1.Attributes.Add( "onclick ", "Display(); ");

-------------------------
哈哈,非常遗憾告诉你,对不据有onclick服务段事件的WebControl,如listbox,使用此方法直接页面声明是可以的!!!
------解决方案--------------------