开发者

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


Try "onclick" instead of "onchange" - I believe that's what you're looking for.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜