2010-05-31 24 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> 

我想通過點擊RadioButtonList使TextBox,而無需使用autopostback=true。我怎樣才能用JavaScript做到這一點?的JavaScript在asp.net

回答

1

您可以使用jQuery來操縱輸入的啓用狀態(TextBox的HTML轉換),或者您可以使用ASP.NET Ajax,以便您可以在更新面板中設置兩個控件,在這種情況下,您將看不到頁面在回發時被重新加載這必須發生,以便您在其他事件中更改TextBox的狀態。 Tbh我會去與ASP.NET Ajax,因爲我的經驗表明,當涉及到複雜的東西,即jQuery不能很好地與ASP.NET控件一起工作。 ASP.NET使用javascript進行事件激活,這可能導致jQuery或ASP.NET無法按照您的預期工作。

祝更新面板...

0

每個列表項呈現具有相同名稱的參數單選按鈕;我會建議運行該應用程序並查看生成的源代碼,以瞭解您需要做什麼才能收聽單選按鈕事件。實質上單選按鈕列表的ID是名稱參數,因此您可以得到一組單選按鈕(使用JQuery):

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

HTH。

0

在這裏你去:

<%@ 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

編寫代碼以下列方式

<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> 

,並禁用文本框(啓用= 「假」),因爲 「RdoBtnHasNotified」 的initialy值沒有」。

1

使用jQuery,您可以通過在掛鉤上的單選按鈕的變化有相當定製的結果......


$("#<%= 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); 
    } 
}); 
我認爲使用變化事件在IE瀏覽器將不觸發
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; 
    } 
});