使用 javascript 选中复选框启用文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4747879/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
enable textbox with checkbox checked with javascript
提问by Laziale
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" />  
       <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
提前感谢大家的帮助,Laziale
回答by adatapost
Please turn of AutoPostBack.
请关闭 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" />
回答by Joe Enos
Try "onclick" instead of "onchange" - I believe that's what you're looking for.
试试“onclick”而不是“onchange”——我相信这就是你要找的。
回答by Prince Antony G
Check this
检查这个
1.If Checkbox is Checked then the Textbox be Disable
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.If Checkbox is Checked then the Textbox be Enable
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" />

