2015-10-28 42 views
0

所以我試圖啓用文本框,如果從下拉列表中選擇其他選項 即,當我選擇其他選項,而不點擊任何按鈕,它顯示文本框 代碼列在下面js文件,但它與這個啓用/禁用基於下拉列表的文本框

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<script src="../Scripts/jquery-1.4.1.min.js"></script> 
<script type="text/javascript"> 
    $('#type_').change(function() { 
    if (this.value == 'other') { 
     $('#other_type').css('display', 'block'); 
    } 
    else { 
     $('#other_type').css('display', 'none'); 
    } 
}); 
</script> 
<div> 
    <asp:Label ID="Label1" runat="server" Text="Type : "></asp:Label> 
    <asp:DropDownList ID="type_" runat="server" style="margin-left: 0px"> 
     <asp:ListItem>a</asp:ListItem> 
     <asp:ListItem>b</asp:ListItem> 
     <asp:ListItem>c</asp:ListItem> 
     <asp:ListItem>other</asp:ListItem> 
    </asp:DropDownList> 
    <asp:TextBox ID="other_type" runat="server" style="margin-left: 33px; display:none" Width="157px"></asp:TextBox>....... 
+0

你可以添加ClientIDMode = Static到你的下拉列表和文本框控件? – g2000

回答

0

首先不靈需要幫助,包裹代碼$(function(){});綁定在頁面加載後點擊。 你沒有正確選擇元素。 ASP .NET更改了ID屬性,因此您必須使用<%= ControlName .ClientID%>來獲取生成的ID。 或者給控件一個CssClass。

$(function(){ 
    $('#<%= type_.ClientID %>').change(function() { 
     if (this.val() == 'other') { 
     $('#<%= other_type.ClientID %>').css('display', 'block'); 
     } 
     else { 
     $('#<%= other_type.ClientID %>').css('display', 'none'); 
     } 
}); 
+0

無法正常工作請檢查我是否完美地鏈接了js文件? –

相關問題