2009-11-26 353 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