道具('disabled',假);不管用

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

我想制作一个按钮,当复选框为 false 时将被禁用。但是 prop('disabled', false); 不起作用。

$(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>

您可以尝试删除该属性。

$('#button').removeAttr('disabled')

在您的代码中,this 指的是 DOM。

改为

$("#agree").change(function() {
    if ($(this).is(':checked')) {
        $('#button').prop('disabled', false);
    } else {
        $('#button').prop('disabled', true);
    }
});

在事件处理程序中,this 指的是触发更改事件的元素。

或者简单地说:

$("#agree").click(function() {
    $('#button').prop('disabled', !$(this).is(':checked'));
});

检查复选框是否被选中 .is(":checked"), 另外,监听复选框本身的更改事件。

下面是稍微修改过的代码(让它在这里工作)

$(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>

要禁用按钮,您可以使用:

$('#button').attr('disabled', 'disabled');

要激活按钮,您可以使用:

$('#button').attr('disabled', '');