[英]How do I have 2 onClick action on javascript?
I'm kind of confused,我有点困惑
I need to have two actions when onClick this {this.startTextHandler} and an alert when click当 onClick 这个 {this.startTextHandler} 和一个警报时,我需要有两个动作
<Form.Item>
{/* <button onClick={this.startTextHandler} className="psButton">START THE TEST</button> */}
<Button onClick={this.startTextHandler} text="START THE TEST" />
</Form.Item>
You could create a function that includes both events:您可以创建一个包含两个事件的函数:
<Button onClick={this.callTwoFuncs} text="START THE TEST" />
<script>
function callTwoFuncs() {
this.startTextHandler();
this.sendAlert();
}
...
</script>
Write one function that does two things.编写一个函数来做两件事。
startTextHandler(e) {
// etc
},
doSomethingElse(e) {
// etc
},
startTextHandlerAndDoSomethingElse(e) {
this.startTextHandler(e);
this.doSomethingElse(e);
}
....
onClick={this.startTextHandlerAndDoSomethingElse}
尝试这个:
<Button onClick={()=> { this.startTextHandler(); this.secondAction(); } } text="START THE TEST" />
You can pass an anonomus function :您可以传递一个匿名函数:
<Button
onClick={(event) => {
this.startTextHandler(event);
this.secondMethod(eventOrWhatever)
}}
text="START THE TEST"
/>
or a named function :或命名函数:
<Button
onClick={this.handleClick}
text="START THE TEST"
/>
handleClick = (event) => {
this.startTextHandler(event);
this.secondMethod(eventOrWhatever);
}
BTW: named function give you a better stacktrace too when debugging顺便说一句:命名函数在调试时也给你一个更好的堆栈跟踪
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.