[英]How do I get conditionals (If/Else) to check if buttons were clicked?
I'm trying to figure out a way of returning different results if a different button is clicked. 我试图找出一种方法,如果单击其他按钮,则返回不同结果。 I have two buttons which are to return values depending on what the user clicks.
我有两个按钮,这些按钮将根据用户单击的内容返回值。 Here is a code that is close to what mine looks like.
这是与我的代码相似的代码。
$(() => { const $btn1 = $("#btn1"), $btn2 = $("#btn2"); let result = 0; function fetchResult() { if("button1 is clicked"){ //Of course, that isn't the condition being tested result = 1 + 2; } else if ("button2 is clicked") { //And neither is this result = 3 + 4; } else { //do nothing } return result; } $("#result").html(fetchResult); });
#result { text-align: center; font-size: 30px; background: green; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div> <button class="btn btn-lg" id="btn1">Button 1</button> <button class="btn btn-lg" id="btn2">Button 2</button> <p id="result"></p> </div>
You have to add an event listener to handle click events for this to work: 您必须添加一个事件侦听器来处理单击事件,才能使其正常工作:
$btn1.on('click', function() {
console.log('button 1 clicked');
});
$btn2.on('click', function() {
console.log('button 2 clicked');
});
Or you could add the event to the .btn
class and determine which one is clicked based on the element's id: 或者,您可以将事件添加到
.btn
类,然后根据元素的ID确定单击哪个事件:
$('.btn').on('click', function() {
if (this.id === 'btn1') {
console.log('button 1 clicked');
} else if (this.id === 'btn2') {
console.log('button 2 clicked');
}
});
The latter approach would probably be better for your particular use case. 对于您的特定用例,后一种方法可能更好。
You could use jQuery .on
method with click
event: 您可以将jQuery
.on
方法与click
事件一起使用:
$("#btn1").on('click', () => { $("#result").html(1 + 2); }); $("#btn2").on('click', () => { $("#result").html(2 + 3); });
#result { text-align: center; font-size: 30px; background: green; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <button class="btn btn-lg" id="btn1">Button 1</button> <button class="btn btn-lg" id="btn2">Button 2</button> <p id="result"></p>
$('button').click(function () {
alert($(this).attr('id'));
// or what ever your code ....
});
Do not forget to add jQuery tag in html file 不要忘记在HTML文件中添加jQuery标签
What about just set variable when some button clicked.. 单击某个按钮时只设置变量呢。
Of course, in your code you will also need to run a function that sends the result to the screen when the button is pressed 当然,在您的代码中,您还需要运行一个函数,当按下按钮时,该函数会将结果发送到屏幕
$(() => {
let btn1Clicked = false,
btn2Clicked = false
const $btn1 = $("#btn1").click(()=>btn1Clicked = true),
$btn2 = $("#btn2").click(()=>btn2Clicked = true);
let result = 0;
function fetchResult() {
if( btn1Clicked ){ //Of course, that isn't the condition
result = 1 + 2;
} else if (btn2Clicked ) { //And this isn't the condition either
result = 3 + 4;
} else {
//do nothing
}
return result;
}
$(document).on('#btn1, #btn2', function(){
fetchResult()
})
});
you could assign onclick events to both of the button individually like 您可以将onclick事件分别分配给两个按钮,例如
$(#btnId1).on('click',function(){
//do your thing here
});
$(#btnId2).on('click',function(){
//do your thing here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.