2011-01-20 65 views
1

HEllo - 我試圖啓用/禁用文本框時,複選框被選中(啓用)或未選中(禁用)。在選中/取消選中複選框之後,我沒有任何事情發生。 這是我有:啓用帶有複選框的javascript文本框

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AssociationInfo.ascx.cs" Inherits="Administration.Modules.AssociationInfo" %> 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> 
    <script type="text/javascript" language="javascript"> 
     function enableTextBox() { 
      window.onload = function() { 
       var check = document.getElementById("chkAssociation"); 
       check.onchange = function() { 
        if (this.checked == true) 
         document.getElementById("txtAddress").disabled = false; 
        else 
         document.getElementById("txtAddress").disabled = true; 
       }; 
      }; 
     } 
    </script> 

    <div> 
    <h2>Association Info</h2> 
    <br /> 

     <asp:CheckBox Checked="false" ID="chkAssociation" runat="server" />&nbsp;&nbsp; 
     <asp:TextBox ID="txtAddress" Text="Test" runat="server" /> 
    </div> 

該代碼是在網絡用戶控制。這可能是爲什麼工作不正常的原因?

感謝您提前幫助

感謝大家的幫助,Laziale

回答

2

嘗試「點擊」,而不是「平變化」 - 我相信這是你在找什麼。

4

請打開的AutoPostBack的。

<asp:CheckBox Checked="false" 
       OnChange="javascript:enableTextBox();" 
       ID="chkAssociation" 
       runat="server" /> 

編輯:試試這個代碼,

<script type="text/javascript"> 
     window.onload = function() { 
      var check = document.getElementById("<%=chkAssociation.ClientID %>"); 
      check.onchange = function() { 
       if (this.checked == true) 
        document.getElementById("<%=txtAddress.ClientID %>").disabled = false; 
       else 
        document.getElementById("<%=txtAddress.ClientID %>").disabled = true; 
      }; 
     }; 
</script>   

<asp:CheckBox Checked="false" ID="chkAssociation" runat="server" /> 
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" /> 
+0

相同,什麼都沒有發生 – Laziale 2011-01-20 14:01:24

+0

您正在使用哪種瀏覽器? – adatapost 2011-01-20 14:17:46

1

入住這

1.如果複選框被選中,則文本框是禁用

<script type="text/javascript"> 
function enableDisable(bEnable, textBoxID) 
{ 
    document.getElementById(textBoxID).disabled = bEnable 

} 
</script> 


<asp:TextBox ID="t1" Text="" runat="server" /> 
<asp:CheckBox ID="chk1" Checked="false" onclick="enableDisable(this.checked, 't1');" runat="server" /> 

2.如果複選框被選中,則文本框會啓用

<script type="text/javascript"> 
function enableDisable(bEnable, textBoxID) 
{ 
    document.getElementById(textBoxID).disabled = !bEnable 

} 
</script> 
     <asp:TextBox ID="t1" Text="" runat="server" /> 
     <asp:CheckBox ID="chk1" Checked="true" onclick="enableDisable(this.checked, 't1');" runat="server" />