简体   繁体   English

如何通过复选框启用/禁用按钮并在刷新页面后保持按钮处于活动状态或非活动状态

[英]How to enable/disable a button via checkbox and keep the button active or inactive after refreshing the page

I have a button and I want to do the following我有一个按钮,我想执行以下操作

  • Disable this button by clicking on the checkbox that refers to it通过单击引用它的复选框禁用此按钮
  • Keep it disabled even after refreshing the page即使在刷新页面后也将其禁用
  • Do the same, but instead.做同样的事情,但相反。 The button is disabled and now I would like to enable it again by clicking on the checkbox that references it keeping it that way after refreshing the page该按钮已禁用,现在我想通过单击引用它的复选框再次启用它,刷新页面后保持这种状态

I found two references that do exactly what I need, but I don't know how to put the two solutions together.我找到了两个完全符合我需要的参考资料,但我不知道如何将这两个解决方案放在一起。 This and this 这个这个

HTML code HTML代码

<div id="billing">
    <input type="checkbox" id="billing-checkbox" checked>
    <input type="button" value="Hello Javascript!">
</div>

Javascript code Javascript代码

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('billing-checkbox').onchange = toggleBilling;
    }, false);

    function toggleBilling() {
    var billingItems = document.querySelectorAll('#billing input[type="button"]');

    for (var i = 0; i < billingItems.length; i++) {
        billingItems[i].disabled = !billingItems[i].disabled;
    }
    }

    $(function(){
        var test = localStorage.input === 'true'? true: false;
        $('input').prop('checked', test || false);
    });

    $('input').on('change', function() {
        localStorage.input = $(this).is(':checked');
        console.log($(this).is(':checked'));
    });

Thank you so much!非常感谢!

This will give a script error in the snippet, probably because it is a sandbox and doesn't allow for localStorage.这将在代码段中给出脚本错误,可能是因为它是一个沙箱并且不允许使用 localStorage。 But this is tested and works.但这已经过测试并且有效。 See comments in the code for explanations.有关解释,请参阅代码中的注释。 You can set the checkbox on or off and when you refresh the page, it will 'remember' it's state您可以打开或关闭复选框,当您刷新页面时,它会“记住”它的状态

 $(document).ready(function() { // first thing is to set the checkbox based on the localStorage key that we may have set in the past $('#billing-checkbox').prop('checked', localStorage.getItem('buttonDisabled') !== "disabled"); // then we run our toggle billing toggleBilling() // we set up our change handler. $('#billing-checkbox').on('change', toggleBilling); }); function toggleBilling(e) { // we set the disabled based on the check button status $('#billing input[type="button"]').attr('disabled', !$('#billing-checkbox').prop('checked')) // we set the localStorage based on the button disabled localStorage.setItem('buttonDisabled', $('#billing input[type="button"]').attr('disabled')); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="billing"> <input type="checkbox" id="billing-checkbox" checked> <input type="button" value="Hello Javascript!"> </div>

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

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