简体   繁体   中英

Jquery for checkbox doesn't work for model popup

In ModalPopupExtender control I have a checkbox. Once the checkbox is checked, I want the Submit button in the popup to be enabled or else be disabled.

 <asp:CheckBox ID="chkUser" TabIndex="0" runat="server" CssClass="cCheckBox" ClientIDMode="Static" onchange="Checked();"/>

  src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type = "text/javascript"> function Checked() { if(document.getElementById('<%=chkUser.ClientID%>').checked) { document.getElementById('<%=btnSubmit.ClientID%>').EnableSubmitButton = true; } } </script> 

I'm getting the following error.

0x800a1391 - JavaScript runtime error: Checked undefined error.

Move this code to the end of body tag

<script type = "text/javascript">

  function Checked() {
    if(document.getElementById('<%=chkUser.ClientID%>').checked) {
      document.getElementById('<%=btnSubmit.ClientID%>').disabled = true;
    }
  }
</script>

Also you may need to handle checkbox change event.

document.getElementById('<%=chkUser.ClientID%>').onchange = Checked;

Change on click to onchange="Checked(this);" .

<asp:CheckBox  onchange="Checked(this);" ID="chkUser" TabIndex="0" runat="server" CssClass="cCheckBox" ClientIDMode="Static"/>

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

You can use jquery ' change ' event delegation on the checkbox and use it's property checked to determine if to disable the button or not.

$().ready(function(){
      $('#<%=chkUser.ClientID%>').on('change',function(){           
       $('#<%=btnSubmit.ClientID%>').prop("disabled",!$(this).prop("checked"));
      });
});

Example : https://jsfiddle.net/DinoMyte/h712mu20/2/

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