简体   繁体   中英

prop('disabled', false); is not working

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.com/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>

In 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'));
});

您可以尝试删除该属性。

$('#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.com/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', '');

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