admin管理员组文章数量:1334392
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', '');
本文标签: javascriptprop(39disabled39false) is not workingStack Overflow
版权声明:本文标题:javascript - prop('disabled', false); is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742238775a2438546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论