使用javascript禁用HTML中的按钮

Disabling a button in HTML using javascript

本文关键字:按钮 HTML javascript 禁用 使用      更新时间:2024-05-10

在我的游戏中,玩家有一个平衡,当他们点击按钮时,它会下降5000,当他们达到0时,他们会从20000开始,我希望出现一个警报(我有这个功能),并禁用按钮-我无法让它发挥作用,有人能解释一下吗?所有的功能都很完美,顺便说一句,我只是想把禁用按钮添加到"减法"功能中——谢谢!

这是我使用的代码

<script type="text/javascript">
function subtract() {
var Balance = document.getElementById("Balance");
var Fee = parseInt(Balance.value) - 5000;
Balance.value = Fee;
if(Fee <= 0){
alert("To Continue Playing - Purchase More Coins at - LINK");
}
}
</script>

<button class="randombutton"  id="aaa" style="float:left;" onclick= "
 randomImg1(); 
 randomImg2(); 
 randomImg3(); 
 randomImg4(); 
 randomImg5(); 
 randomImg6(); 
 randomImg7(); 
 randomImg8();
 subtract();
 foo(this);"/>OPEN PACK</button>

要使用javascript禁用表单元素,请将disabled属性设置为true

function subtract() {
    var Balance = document.getElementById("Balance");
    var Fee = parseInt(Balance.value) - 5000;
    Balance.value = Fee;
    if(Fee <= 0){
        // Get the element and then set the disabled property to true.
        document.getElementById('aaa').disabled = true;
        alert("To Continue Playing - Purchase More Coins at - LINK");
    }
}