2010-05-31 49 views
6
<asp:RadioButtonList ID="RdoBtnHasNotified" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="RdoBtnHasNotified_SelectedIndexChanged"> 
    <asp:ListItem Value="1">Yes</asp:ListItem> 
    <asp:ListItem Value="0" Selected="True">No</asp:ListItem> 
</asp:RadioButtonList> 


<asp:TextBox ID="TxtHowNotified" runat="server" TextMode="MultiLine" MaxLength="100"></asp:TextBox> 

Tôi muốn bật TextBox bằng cách nhấp vào RadioButtonList, mà không sử dụng autopostback=true. Làm thế nào tôi có thể làm điều này với JavaScript?javascript trong asp.net

Trả lời

1

Bạn có thể sử dụng jQuery để thao tác trạng thái đã bật của đầu vào (HTML dịch cho TextBox) hoặc bạn có thể sử dụng ASP.NET Ajax để có thể đặt cả hai điều khiển bên trong bảng cập nhật trong trường hợp này. điều này phải xảy ra để bạn có thể thay đổi trạng thái của TextBox trên một số sự kiện khác. Tbh tôi sẽ đi với ASP.NET Ajax bởi vì kinh nghiệm của tôi cho thấy rằng jQuery không hoạt động tốt với ASP.NET điều khiển khi nói đến công cụ phức tạp tức là. ASP.NET sử dụng javascript để kích hoạt sự kiện có thể khiến jQuery hoặc ASP.NET không hoạt động như bạn mong đợi.

Chúc may mắn với bảng cập nhật ...

0

Mỗi ListItem hiển thị nút radio có cùng thông số tên; Tôi khuyên bạn nên chạy ứng dụng và xem nguồn được tạo để có ý tưởng về những gì bạn cần làm để nghe các sự kiện nút radio. Về cơ bản ID của danh sách nút radio là tham số tên, do đó bạn có thể nhận nhóm các nút radio dưới dạng (sử dụng JQuery):

$("input[name='<%= rbl.ClientID%>']").click(function() { 
    var tb = $("#textboxid"); 
    //do something here; this points to the radio button 
}); 

HTH.

0

Ở đây bạn đi:

<%@ Page Language="C#" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 
    protected void RdoBtnHasNotified_PreRender(object sender, EventArgs e) 
    { 
     foreach (ListItem item in RdoBtnHasNotified.Items) 
      item.Attributes.Add("onclick", string.Format("toggleTextBox(this,'{0}');", TxtHowNotified.ClientID)); 
    } 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 

    <script type="text/javascript"> 
     function toggleTextBox(radioButton, textBoxId) { 
      document.getElementById(textBoxId).disabled = radioButton.value != "1"; 
     } 
    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:RadioButtonList ID="RdoBtnHasNotified" OnPreRender="RdoBtnHasNotified_PreRender" 
      runat="server" RepeatDirection="Horizontal"> 
      <asp:ListItem Value="1">Yes</asp:ListItem> 
      <asp:ListItem Value="0" Selected="True">No</asp:ListItem> 
     </asp:RadioButtonList> 
     <asp:TextBox ID="TxtHowNotified" runat="server" TextMode="MultiLine" MaxLength="100" Enabled="false"></asp:TextBox> 
    </div> 
    </form> 
</body> 
</html> 
0

Viết mã theo cách sau

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("input[name='RdoBtnHasNotified']").change(function() { 
     $("input[name='RdoBtnHasNotified']:checked").val() == '1' ? $('#TxtHowNotified').removeAttr("disabled") : $('#TxtHowNotified').attr('disabled', 'true'); 

     }); 
    }); 

</script> 

và cũng vô hiệu hóa các textbox (Enabled = "false") kể từ khi khởi tạo là giá trị của "RdoBtnHasNotified" là "Không".

1

Sử dụng jQuery, bạn có thể có một kết quả khá tùy chỉnh bằng cách gắn vào thay đổi nào trên các nút radio ...


$("#<%= RdoBtnHasNotified.ClientID %> > input[type=radio]").change(function(){ 
    // this function is called whenever one of the radio button list's control's change 
    // the $(this) variable refers to the input control that triggered the event 
    var txt = $("#<%= TxtHowNotified.ClientID %>"); 
    if($(this).val()=="1") { 
    txt.removeAttr("disabled"); 
    } else { 
    txt.attr("disabled", true); 
    } 
}); 
0
$('#<%= RdoBtnHasNotified.ClientID %> > input[type=radio]').click(function() 
{ 
    var txtbox = $('#<%= TxtHowNotified.ClientID %>'); 
    if($(this).val() == '1') 
    { 
    document.getElementById('#<%= TxtHowNotified.ClientID %>').disabled = false; 
    } 
    else 
    { 
    document.getElementById('#<%= TxtHowNotified.ClientID %>').disabled = true; 
    } 
}); 

Tôi nghĩ rằng sử dụng sự kiện thay đổi sẽ không sa thải trong IE.

Các vấn đề liên quan