使用javascript选中的复选框启用文本框 [英] enable textbox with checkbox checked with javascript

查看:83
本文介绍了使用javascript选中的复选框启用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0 down vote favorite

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:
<script type="text/javascript" language="javascript">
    function enableTextBox() {
        if (document.getElementById("chkAssociation").checked == true)
            document.getElementById("txtAddress").disabled = false;
        else
            document.getElementById("txtAddress").disabled = true;
    }
</script>
and than the control:
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />&nbsp;&nbsp;
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />




感谢您的帮助




Thanks for helping

推荐答案

我发现了您的问题. asp:checkbox标记中的ID属性与客户端(HTML)标记的ID属性不同.我会尝试纠正您的代码,但必须说,由于我无法尝试执行此操作,因此可能需要对您进行一些调整:

I spotted you problem. The ID attribute in the asp:checkbox tag is not the same as the ID attribute of the client side (HTML) tag. I''ll try to rectify your code, but must say that since I can''t try this out it might need some tweaking on your side:

<script type="text/javascript" language="javascript">
    function enableTextBox() {
        var textBoxID = "<%= txtAddress.ClientID %>";
        if (document.getElementById("<%= chkAssociation.ClientID %>").checked == true)
            document.getElementById(textBoxID).disabled = false;
        else
            document.getElementById(textBoxID).disabled = true;
    }
</script>
and than the control:
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />



您必须真正查看如何从服务器端的控件中获取客户端ID.它可能是我所指出的属性,但也可能是一种方法.要点在于,尽管服务器端asp:textbox和asp:checkbox标记中的ID属性 NOT 与将输出输出到呈现的HTML中的ID属性相同.

希望对您有帮助!

最好的问候,
Manfred



You''ll have to look it up really how the client ID is fetched from the control on the server side. It could be a property like I indicated, but might as well be a method. Main point is though that the ID attribute in the server side asp:textbox and asp:checkbox tags is NOT the same as the ID attribute that gets output into the rendered HTML.

Hope that helps you!

Best Regards,
Manfred


代码绝对正确.

希望您在适当的位置添加了javascript,请参考此代码段.

Code is absolutely fine.

Hope you have added javascript at proper place, please refer this code snippet.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function enableTextBox()
    {
        if (document.getElementById("chkAssociation").checked == true)
            document.getElementById("txtAddress").disabled = false;
        else
            document.getElementById("txtAddress").disabled = true;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />
        <asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />
    </div>
    </form>
</body>
</html>


这篇关于使用javascript选中的复选框启用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆