How to trigger an event after clicking two different button in Javascript

风流意气都作罢 提交于 2019-12-13 02:49:50

问题


I was wondering how I can trigger an event after two different buttons being clicked. Specifically I have some buttons with different ids and I want when I click two specific buttons from them to trigger an event. I thought that this can be happen If I call some function in the first on click event and inside this function I can call another function if the second button being clicked. It does not seem to work, so I need your help. Thanks. (if anyone needs more code please comment to upload the whole code)

     function someKindOfFunction(){  
         //there is some other code here

         var ob = document.getElementById(idTable[0]); 
         var ob1 = document.getElementById(idTable[1]); 
         var ob2 = document.getElementById(idTable[2]); 
         var ob3 = document.getElementById(idTable[3]); 


         ob.addEventListener("click",function() {onCl1(idTable[0],idTable)});
         ob1.addEventListener("click",function() {onCl1(idTable[1],idTable)});
         ob2.addEventListener("click",function() {onCl1(idTable[2],idTable)});
         ob3.addEventListener("click",function() {onCl1(idTable[3],idTable)});


    }

    function onCl1(id1,idTable){
        var obj1 = document.getElementById(id1);
        obj1.disabled=true;
        var obj2,v,obj3,obj4;
        v=0;
        var temp = ["0","0","0"];
        for(var i =0 ; i<4 ; i++){
          if( id1 != idTable[i]){
            temp[v] = idTable[i];
            v=v+1;
          }
        }
        ob=document.getElementById(temp[0]);
        ob1=document.getElementById(temp[1]);
        ob2=document.getElementById(temp[2]);

        ob.addEventListener("click",function() {onCl2(id1,temp[0])});
        ob1.addEventListener("click",function() {onCl2(id1,temp[1])});
        ob2.addEventListener("click",function() {onCl2(id1,temp[2])});


      }

      function onCl2(id1,id2){
          //some kind of actions
          alert("it wokrs");

      }

回答1:


Just pass some sort of value whenever the button you want is clicked. For instance:

 ob.addEventListener("click",function() {onCl1(1)});
 ob1.addEventListener("click",function() {onCl1(1)});
  var i;   
function onCl1(value){
i += value;
if(i == 2){
//do this
   }
}

So basically once these two buttons are clicked the value will equal 2 and trigger whatever you want.



来源:https://stackoverflow.com/questions/23415344/how-to-trigger-an-event-after-clicking-two-different-button-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!