简体   繁体   English

如果启用了另一个按钮,如何编写知道单击了一个按钮并执行某些操作的函数?

[英]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.

相关问题 如何知道是否在另一页上单击了按钮? - How to know if button has been clicked on another page? 单击按钮后如何获得执行功能的按钮 - How to get a button to perform a function when it has been clicked 如何声明在onclick函数之外已单击按钮 - How to declare that a button has been clicked outside the onclick function 测试是否单击了按钮 - Testing if a button has been clicked or not 如何使在单击按钮一定次数后执行的功能? - How to make a function that executes when a button is clicked a certain amount of times? 如果曾经点击过按钮,则运行一个函数,即使在页面重新加载时 - Run a function if a button has ever been clicked, even on page reload 单击另一个页面上的按钮后如何启动计时器? - How do I start a timer after a button on another page has been clicked? 在 stencil js 中,我如何检查按钮是否在不同类的另一个组件中被单击 - In stencil js, how can i check if button has been clicked in another component from a different class 如何制作一个 function 来跟踪单击按钮的次数 - How to make a function that keep tracks of the amount of times a button has been clicked 在DOM上单击按钮后如何调用函数 - How do i call a function after a button has been clicked on DOM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM