2009-11-26 132 views

回答

2

将SelectionMode属性设置为Single的列表框使用以下代码。

var istBoxElement = document.getElementById ("ListBox1"); 
var textBoxElement = document.getElementById ("txtBox1");  

textBoxElement.value = elem.value; 

并将其写入文本框元素的onfocus事件中。

示例代码

<script type="text/javascript"> 
    window.onload = function() { BindEvents(); } 

    function BindEvents() 
    { 
     var textBx = document.getElementById ("txt1"); 
     textBx.onfocus = function() { 
      SetSel(this); 
     } 
    } 

    function SetSel (elem) 
    { 
     alert (elem.id); 
     var elem = document.getElementById ("ListBox1"); 
     document.getElementById ("txt1").value = elem.value; 
    } 
</script> 

<div id="divMain"> 
    <input type="text" id="txt1" /> 
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
     <asp:ListItem>3</asp:ListItem> 
    </asp:ListBox> 
</div> 
+0

如何获得焦点的文本框的ID – 2009-11-26 09:09:17

0

我认为你正在寻找onBlur。您可以在查看最后一个项目后保留一个变量,并使用JavaScript来设置文本。

+0

詹姆斯我没有得到你..一些前plz – 2009-11-26 09:07:36