简体   繁体   中英

JavaScript button Enable and Disable

Hello there so I'm making a remake of the popular game cookie clicker but I'm having some issues so here is what I need help with

So as you know in the game cookie clicker, they have power up buttons and those buttons disable them selfs when you do not have enough money to buy it

and that's what I need help with I want to be able to have the power-up buttons disabled and when I have enough money make them auto enable

so here I'll put some example code to sort of explain it a bit better

code:

if (cookies >= 10) {
    document.getElementById("IdName").disabled = false;
}else {
    document.getElementById("IdName").disabled = true;
}

Something like this?

 function evalButton() { var cookies = document.getElementById("cookies").value; cookies = parseInt(cookies, 10); console.log(cookies); if (cookies >= 10) { document.getElementById("IdName").disabled = false; }else { document.getElementById("IdName").disabled = true; } } 
 #IdName { background: red; } #IdName:disabled { background: grey; } 
 <input id="cookies" onkeyup="evalButton()"/> <button id="IdName">Press me</button> 

Your code is working fine-- and cookies value can be derived in the function or passed as an argument. A bit of CSS can be used to give visual cues to the disabled state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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