簡體   English   中英

prop('禁用', false); 不工作

[英]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>

在您的代碼中, 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'));
});

您可以嘗試刪除該屬性。

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

檢查復選框是否被.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', '');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM