首页 > 解决方案 > BS4:用按钮禁用按钮

问题描述

我目前正在尝试为我正在开发@work 的项目自学BS4。基本上我所拥有的是一个按钮组(2 个。左侧是活动的,右侧是禁用的) 我想要做的是禁用左侧并激活右侧,反之亦然。这是代码

<div class="btn-group">
     <button type="button" id="showscore" class="btn btn-success">Show</button>
     <button type="button" id="hidescore" class="btn btn-danger" disabled>Hide</button>
</div>

和 JS:

function lockbutton() {
    var x = document.getElementById("#showscore").button;
    if(x.click()) {
        document.getElementById("hidescore").disabled = true;
    } else{
        document.getElementById("hidescore").disabled = false; 
    }
}

不要为 JS/jQuery 代码烤我。我已经知道错了。但我找不到解决方法。

标签: javascriptjqueryfunctionbuttonbootstrap-4

解决方案


您可能希望使用 bootstrap 中包含的 jQuery 库。

$(document).ready(function() {

    $('#showscore').click(function() {
        $('#hidescore').prop('disabled', false);
        $(this).prop('disabled', true);
    });

    $('#hidescore').click(function() {
        $('#showscore').prop('disabled', false);
        $(this).prop('disabled', true);
    });

});

推荐阅读