[英]prop('disabled', false); is not working
I want to make a button that will be disable when checkbox is false.我想制作一个在复选框为 false 时将被禁用的按钮。 But
prop('disabled', false);
但是
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.在您的代码中,
this
指的是 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.在事件处理程序中,
this
指的是触发更改事件的元素。
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.检查复选框是否被
.is(":checked")
,同时,监听复选框本身的更改事件。
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', '');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.