简体   繁体   中英

Change button's enabled property via checkbox

I tried this JavaScript but it doesn't work - here

I need to change the button's enabled property to true when the checkbox is checked and to false when it isn't. This is my code:

<tr>
  <td colspan="2" align="center">
    <asp:CheckBox ID="cbAcceptAgreement" runat="server" OnClientClick="acceptAgreement(this)" />
    <asp:Label ID="lblUserAgreement" runat="server" Text="I accept the " />
    <asp:HyperLink ID="hlUserAgreement" runat="server" Text="User Agreement" NavigateUrl="Help/UserAgreement.aspx" />
  </td>
</tr>
<tr>
  <td colspan="2" align="center">
    <asp:Button ID="btnRegister" runat="server" Text="Register"  />
  </td>
</tr>

<script type="text/javascript">
  function acceptAgreement(obj) {
    document.getElementById('<%=btnRegister.ClientID%>').disabled = !obj.checked;
  }
</script>

Can you help me solve this problem?

Javascript is case sensitive, you have Obj and obj in your function, they must match:)

To fix, change your function to this:

function acceptAgreement(obj) {
  document.getElementById('<%=btnRegister.ClientID%>').disabled = !obj.checked;
}
function acceptAgreement(id){ // where id is check box id
  var check = document.getElementById(id).checked
  document.getElementById('btnRegister').disabled = !check
}

since you have these buttons and checkbox running at server why dont you handle the enabling and disabling with the server code behind. Also which programming language are you using for the codebehind c# example

            protected void cbAcceptAgreement_CheckChanged(object sender, EventArgs e)
        {
             btnRegister.Enabled = cbAcceptAgreement.Checked;
        }

Page part

        <asp:CheckBox ID="cbAcceptAgreement" runat="server" AutoPostBack="True"
        oncheckedchanged="cbAcceptAgreement_CheckedChanged" />

lemme know if this worked for you Ivan

changed to autopostback = true for the chekcbox hopefully that will help

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