简体   繁体   English

通过复选框更改按钮的启用属性

[英]Change button's enabled property via checkbox

I tried this JavaScript but it doesn't work - here我试过这个 JavaScript 但它不起作用 - 这里

I need to change the button's enabled property to true when the checkbox is checked and to false when it isn't.我需要在选中复选框时将按钮的启用属性更改为 true,在未选中时更改为 false。 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:) Javascript 区分大小写,你的 function 中有Objobj ,它们必须匹配:)

To fix, change your function to this:要修复,请将您的 function 更改为:

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您还为代码隐藏 c# 示例使用了哪种编程语言

            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将 chekcbox 更改为 autopostback = true 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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