How to disable controls with JQuery when loading page

0

Include the JS Code below to disable fields when the checkbox is checked. However, when I load the page with it checked the fields need to be disabled, and the command only disables them when the click occurs. How can I disable the controls (textbox) with page load?

<script type="text/javascript">
    $(document).ready(function ()
    {
        $('#chkContaProvisoria').click(function ()
        {
            if (this.checked)
            {
                $('#DadosConta_Conta_DtValidade').attr("disabled", "disabled");
                $('#DadosConta_Conta_Numero').attr("disabled", "disabled");
            }
            else
            {
                $('#DadosConta_Conta_DtValidade').removeAttr("disabled")
                $('#DadosConta_Conta_Numero').removeAttr("disabled")
            }
        });
    });
</script>

Form submission with fields

    
asked by anonymous 29.01.2016 / 13:52

1 answer

1

You can do it that way

$(document).ready(function(){
  $('#chkContaProvisoria').click(function ()
    {
        desabilitaTextBox();
    });

    //chama a função apos o carregamento da pagina, 
    //se o checkbox estiver marcado ele desabilita o campo.
    desabilitaTextBox();

});

function desabilitaTextBox() {
    if ($('#chkContaProvisoria').is(':checked')) {
         $('#DadosConta_Conta_DtValidade').attr("disabled", "disabled");
         $('#DadosConta_Conta_Numero').attr("disabled", "disabled");
     } else {
         $('#DadosConta_Conta_DtValidade').removeAttr("disabled");
         $('#DadosConta_Conta_Numero').removeAttr("disabled");
     }
}
    
29.01.2016 / 14:07