简体   繁体   English

启用禁用的复选框,然后使用jQuery单击它

[英]Enable disabled checkbox and then click it with jquery

I try to enable a checkbox and then click it via code. 我尝试启用一个复选框,然后通过代码单击它。 But it doesn't work. 但这是行不通的。 If I click the enable and click checkbox button the checkbox toggles. 如果单击enable and click checkbox按钮,则复选框会切换。 But if I press disable checkbox first it only gets enabled and not clicked by pressing enable and click checkbox button. 但是,如果我先按“ disable checkbox则它只会被启用,而不能通过按“ enable and click checkbox按钮来enable and click checkbox

 function disableCheckbox() { $("#restprovider").prop("disabled", true); } function enableCheckboxAndClick() { $("#restprovider").prop("disabled", false); $("#restprovider").click(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="restprovider" type="checkbox"> <button onclick="disableCheckbox()"> disable checkbox </button> <button onclick="enableCheckboxAndClick()"> enable and click checkbox </button> 

The code ( $("#restprovider").click(); ) works in Chrome but not in eg Firefox v.58. 代码( $("#restprovider").click(); )在Chrome中有效,但在Firefox v.58等版本中无效。

The issue appears to be related to the fact that Firefox doesn't recognize the checkbox as enabled until after a redraw is made. 该问题似乎与以下事实有关:Firefox在重新绘制后才将checkbox识别为已启用。

One option to fix that is to wrap the click() call in a setTimeout() , as shown in below sample, but there are several more options: 一种解决方法是将click()调用包装在setTimeout() ,如下面的示例所示,但是还有更多选项:

Note, the here used timeout value of 4ms might not work cross browser, so its value might has to be adjusted a little. 请注意,此处使用的4ms超时值可能无法在跨浏览器中正常运行,因此可能需要对其进行一些调整。

Stack snippet 堆栈片段

 function disableCheckbox() { $("#restprovider").prop("disabled", true); } function enableCheckboxAndClick() { $("#restprovider").prop("disabled", false); setTimeout(function() { $("#restprovider").click(); }, 4) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="restprovider" type="checkbox"> <button onclick="disableCheckbox()"> disable checkbox </button> <button onclick="enableCheckboxAndClick()"> enable and click checkbox </button> 


Updated after a question edit 在问题编辑后更新

If you are only interested eg in toggling the input 's checked state, simply do like this: 如果您仅对切换input的检查状态感兴趣,只需执行以下操作:

 function disableCheckbox() { $("#restprovider").prop("disabled", true); } function enableCheckboxAndClick() { $("#restprovider").prop("disabled", false); $("#restprovider").prop("checked", function() { this.checked = !this.checked }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="restprovider" type="checkbox"> <button onclick="disableCheckbox()"> disable checkbox </button> <button onclick="enableCheckboxAndClick()"> enable and click checkbox </button> 

Instead of 代替

  $("#restprovider").click();

Use 采用

 $("#restprovider").prop( "checked", true );

Now It won't toggle. 现在,它不会切换。

 function disableCheckbox() { $("#restprovider").prop("disabled", true); } function enableCheckboxAndClick() { $("#restprovider").prop("disabled", false); $("#restprovider").prop( "checked", true ); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="restprovider" type="checkbox"> <button onclick="disableCheckbox()"> disable checkbox </button> <button onclick="enableCheckboxAndClick()"> enable and click checkbox </button> 

One way is to use checked property instead: 一种方法是改为使用选中的属性:

var $restprovider = $('#restprovider');

function disableCheckbox() {
    $restprovider.prop("disabled", true);
}

function enableCheckboxAndClick() {  
    $restprovider.prop('disabled', false);
    $restprovider[0].checked 
        ? ($restprovider.click(), $restprovider.prop('checked', false)) 
        : $restprovider.prop('checked', true);
}

And here's an example . 这是一个例子

your code works good. 您的代码效果很好。 you can resimple code with: 您可以使用以下方式重新实现代码:

$("#restprovider").prop("disabled", false).click();

如果您想要一个看起来已被禁用的复选框,则可以这样操作:

$('#restprovider').css({pointerEvents:"none", opacity:0.5});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM