Doubt on using checked="checked" in a jquery function

1

I have a form and wanted that when the page was loaded, if my checkbox is set, disable the "disable" property of the form.

In the code example below, I put checked="checked" but it only works if I click to disable and click again to enable.

Well lost in it.

Could you help me?

$('#meuCheckbox').click(function() {
    $('#meuTexto').attr('disabled',! this.checked)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script><inputtype="text" disabled="disabled" id="meuTexto" /><input type="checkbox" id="meuCheckbox" checked="checked" />
    
asked by anonymous 11.10.2016 / 01:42

1 answer

3

Use the $(document).ready(function() {}); that will define the actions when the page loads.

$('#meuCheckbox').click(function() {
    $('#meuTexto').attr('disabled', !this.checked)
});

$(document).ready(function() {
    $('#meuTexto').attr('disabled', !$('#meuCheckbox').is(':checked'))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script><inputtype="text" disabled="disabled" id="meuTexto" />
<input type="checkbox" id="meuCheckbox" checked="checked" />
    
11.10.2016 / 01:47