简体   繁体   English

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

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

相关问题 jQuery .prop(“disabled”,false)未启用输入 - jQuery .prop(“disabled”, false) is not enabling an input jquery .prop(&#39;disabled&#39;,true)无效 - jquery .prop('disabled', true) not working jQuery .prop(“ disabled”,true)无法正常工作 - jQuery .prop(“disabled”,true) not working .prop(&quot;禁用&quot;, true); ,prop(&quot;禁用&quot;, &#39;禁用&#39;); 和 .attr(&#39;disabled&#39;, &#39;disabled&#39;) 在 chrome 中不起作用 - .prop("disabled", true); ,prop("disabled", 'disabled'); and .attr('disabled', 'disabled') is not working in chrome VUE - “禁用”的道具绑定在 select 字段上不起作用 - VUE - prop binding for "disabled" not working on select field 如果在jQuery中检查prop(“ disabled”)的语句不起作用 - If statement to check prop(“disabled”) in jquery not working 为什么我的禁用道具没有按预期工作? - Why is my disabled prop not working as expected? jQuery prop(&#39;required&#39;,false,(!$(this).is(&#39;:checked&#39;))不起作用 - JQuery prop('required', false, (!$(this).is(':checked')) not working 的setTimeout(“this.disabled =假”,3000); 不管用 - setTimeout(“this.disabled=false”,3000); is not working jQuery prop()方法有效,但未向元素添加“禁用”属性 - jQuery prop() method working, but don't added attribute “disabled” to element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM