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