简体   繁体   中英

How to write a function that knows a button has been clicked that executes something if another button is enabled?

I have three buttons and the last two are disabled and the first is enabled. After clicking the stop button, the first and last then become disabled and the middle button becomes enabled. At each click of the stop button a message pops up. I want the stop button to know that it's on the middle button and that its enabled so it has its unique message pop up.

I have tried

//html
<div class="Input" id="Input">
        <div class="onOff">
            <button id="on" class="on" onclick="activate(); 
gillespie();">Begin Simulation</button>
            <button id="stop" class="off" onclick="disableA(); 
stopDesc()">Stop Simulation</button>
        </div>
<div class="mRNAbutton">
            <button id = "rnaLow" class="mRNAlow" onclick="fmRNAlow()">[Low 
mRNA]</button>
            <button id = "rnaMedium" class="mRNAmed" onclick="fmRNAmed()"> 
[Medium mRNA]</button>
            <button id = "rnaHigh" class="mRNAhigh" onclick="fmRNAhigh()"> 
[High mRNA]</button>
        </div>
//Javascript
function ifEnabled()
{
var ifEnab = document.getElementById('rnaMedium');

if(ifEnab.enabled &&  document.getElementById('stop').clicked == true){

//random code
}

The expected result is the stop button being able to give me different pop-up messages based on which button is enabled at that time.

This will remember what button was last clicked and display an alert accordingly.

 var message; function lastClicked(btn) { message = btn.id; } function off() { alert(message); } 
 <button id="Low" onclick="lastClicked(this)">Low</button> <button id="Medium" onclick="lastClicked(this)">Medium</button> <button id="High" onclick="lastClicked(this)">High</button> <button id="Off" onclick="off()"><b>Off</b></button> 

Here's a working example on how you can trigger some action if a button has been clicked when another one was disabled:

 <button id="btn-1" onClick="hello(this)"> One </button> <button id="btn-2" onClick="hello(this)"> Two </button> <button id="btn-3" onClick="hello(this)"> Three </button> <script> const btn1 = document.getElementById('btn-1'); const btn2 = document.getElementById('btn-2'); const btn3 = document.getElementById('btn-3'); const hello = (element) => { // Disable whichever button is clicked element.disabled = true if(btn2.disabled && btn3.disabled) { alert("I'll do something when btn2 and btn3 are disabled") } else if(btn1.disabled && btn2.disabled) { alert("I'll do something when btn1 and btn2 are disabled") } } </script> 

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