I want to make a button that will be disable when checkbox is false. But prop('disabled', false);
isn't working.
$(function() {
$(this).click(function() {
if ($('#аgree').prop(':checked')) {
$('#button').prop('disabled', false);
} else {
$('#button').prop('disabled', true);
}
});
});
<script src=".1.1/jquery.min.js"></script>
<p class="text-center">З правилами анкетування ознайомлений(а) і згоден(на) @Html.CheckBox("Agree", false, new { id = "agree" }) </p>
<p class="text-center"><input type="button" id="button" disabled="disabled" value="Пройти анкетування" onclick="location.href='@Url.Action("Form")'" /></p>
I want to make a button that will be disable when checkbox is false. But prop('disabled', false);
isn't working.
$(function() {
$(this).click(function() {
if ($('#аgree').prop(':checked')) {
$('#button').prop('disabled', false);
} else {
$('#button').prop('disabled', true);
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="text-center">З правилами анкетування ознайомлений(а) і згоден(на) @Html.CheckBox("Agree", false, new { id = "agree" }) </p>
<p class="text-center"><input type="button" id="button" disabled="disabled" value="Пройти анкетування" onclick="location.href='@Url.Action("Form")'" /></p>
Share
Improve this question
edited Oct 10, 2017 at 20:27
CDspace
2,68919 gold badges32 silver badges39 bronze badges
asked Oct 10, 2017 at 20:21
Dmytro PetrenkoDmytro Petrenko
231 gold badge1 silver badge6 bronze badges
0
4 Answers
Reset to default 3In your code, this
refers to the DOM.
Change it to
$("#agree").change(function() {
if ($(this).is(':checked')) {
$('#button').prop('disabled', false);
} else {
$('#button').prop('disabled', true);
}
});
Inside the event handler, this
refers to the element that triggered the change event.
or simply:
$("#agree").click(function() {
$('#button').prop('disabled', !$(this).is(':checked'));
});
You can try removing the attribute.
$('#button').removeAttr('disabled')
Check if the checkbox is checked with .is(":checked")
,
also, listen to a change event on the checkbox itself.
Below is a little modified code (to make it work here)
$(function() {
$('#agree').change(function() {
if ($(this).is(':checked')) {
$('#button').prop('disabled', false);
} else {
$('#button').prop('disabled', true);
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="text-center">З правилами анкетування ознайомлений(а) і згоден(на) <input type=checkbox id=agree> </p>
<p class="text-center"><input type="button" id="button" disabled="disabled" value="Пройти анкетування" onclick="location.href='@Url.Action("Form")'" /></p>
To disable button, you can use:
$('#button').attr('disabled', 'disabled');
To make button active, you can use:
$('#button').attr('disabled', '');