简体   繁体   中英

enable textbox with checkbox checked with javascript

HEllo - I am trying to enable/disable textbox when checkbox is checked (enable) or unchecked (disable). WIth the piece of code i have nothing is happening once the checkbox is checked/unchecked. Here is what I have:

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

The code is in web user control. Could that be reason why is not working correctly?

Thanks for helping

Thanks everyone for the help in advance, Laziale

Please turn of AutoPostBack.

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

EDIT: Try this code,

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

尝试使用“ onclick”而不是“ onchange”-我相信这就是您想要的。

Check this

1.If Checkbox is Checked then the Textbox be Disable

<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.If Checkbox is Checked then the Textbox be Enable

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM