简体   繁体   English

我如何获得条件(如果/其他)来检查是否单击了按钮?

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

相关问题 如果单击 3 个按钮,如何“做某事”? (我试过这个:) - How to "do something" if 3 buttons clicked ? (I tried with this : ) 我怎样才能让按钮检查其他按钮是否被点击 - How can i make the Button check if other buttons clicked 在我终于找到一种方法来获取事件点击按钮的 id 之后,其他一切都停止了工作 - After I finally find a way to get id's of the clicked buttons with the event, everthing else stopped working 如何检查邮件是否已成功本地保存到Redis? - How do I check if messages were successfully saved to Redis locally? 创建并单击后如何删除按钮 - How do i delete buttons after theyre created and clicked 检查是否单击了按钮,执行其他操作 - check whether the button is clicked do something else do nothing 反应应用程序:所有三个按钮都被自己点击,我如何阻止这种情况发生(反应非常新) - React application: all three buttons get clicked by themselves, how do I stop that from happening (very new to react) 如何获得单击按钮的索引? - How do I get the index of a clicked button? 如何加载节点模块,就像当前模块位于其他位置一样? - How do I load a node module as if the current module were located somewhere else? 如果无法单击 png 图像的外部并且只能单击实际的“图像”部分,我该如何制作? - How do I make it were the outside of my png image cannot be clicked and only the actual "image" part can be clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM