2010-01-03 31 views
0

我有一個JavaScript來調用時啓用文本框,當用戶從下拉列表中選擇值「自定義」時我想觸發此代碼,以便我可以顯示/隱藏這些新的文本框。來自ASP控件的客戶端JavaScript調用不會重置知名度

function setVisibility(DropDownListID) { 
    var element = document.getElementById(DropDownListID); 
    var element1 = document.getElementById("TestBox1"); 
    if (element.value == "Custom") { 
     element1.visible = !element1.visible ; 
    } 

<asp:DropDownList ID="DateRangeDropDownList" runat="server" Enabled="True" onChange="setVisibility('DateRangeDropDownList');"> 
        <asp:ListItem>Some Value</asp:ListItem> 
         <asp:ListItem>Custom</asp:ListItem> 
        </asp:DropDownList> 

<asp:TextBox ID="TestBox1" runat="server"></asp:TextBox> 

由於某種原因,它似乎沒有工作。有任何想法嗎?

回答

0

由於ASP.NET render的控件與您在函數調用中指定的不同。使用控件的ClientID屬性爲服務器端控件獲取呈現的id屬性。在這裏,我把下拉本身作爲參數,並通過它訪問您的文本框:ClientID

function setVisibility(yourDropdown) { 
var element1 = document.getElementById(<%= TestBox1.ClientID %>); 
if (yourDropdown.value == "Custom") { 
    element1.visible = !element1.visible ; 
} 

<asp:DropDownList ID="DateRangeDropDownList" runat="server" Enabled="True" 
    onChange="setVisibility(this);"> 
     <asp:ListItem>Some Value</asp:ListItem> 
     <asp:ListItem>Custom</asp:ListItem> 
</asp:DropDownList> 
<asp:TextBox ID="TestBox1" runat="server"></asp:TextBox> 
相關問題