简体   繁体   English

如何使带有呼叫功能的多个按钮全部起作用

[英]How to make several buttons with call functions all work

I'm trying to have multiple buttons which each have an individual call function, but either all the buttons do the same thing or none of them work at all. 我正在尝试有多个按钮,每个按钮都有一个单独的调用函数,但是所有按钮要么做同样的事情,要么根本不起作用。 So this is my first button: 这是我的第一个按钮:

<button id="study" onclick="call();">Study</button>

And this is its script: 这是它的脚本:

var callCount = 0;

function one() {
   changeImage1('Call one');
   {
    var img = document.getElementById("image");
    img.src="images/tumblr_dash.gif";
   document.getElementById("action").innerHTML = "You have decided to study";
   }
}

function two() {
   changeImage2('Call two');
   {
   var img = document.getElementById("image");
   img.src="images/tumblr_dash.gif";
   document.getElementById("action").innerHTML = "You have decided to start studying in a minute";
   }
}
function three() {
   changeImage3('Call three');
   {
   var img = document.getElementById("image");
   img.src="images/study.gif";
   document.getElementById("action").innerHTML = "You are currently studying";
   }
}

function call(){
  callCount++;
  var btn = document.getElementById('study');
   if(callCount == 1) one();
   else if(callCount == 2) two();
  else if(callCount == 3) three();
  else btn.disabled = true;

  //callOne = false /*!callOne;*/
}

And that works fine. 而且效果很好。 But when I put in my second button: 但是当我按下第二个按钮时:

<button id="eat" onclick="call1();">Eat</button>

Which has this script: 具有以下脚本:

var callCount1 = 0;

function one1() {
   changeImage1('Call one');
   {
    var img = document.getElementById("image");
    img.src="images/tumblr_dash.gif";
   document.getElementById("action").innerHTML = "You have decided to study";
   }
}

function two1() {
   changeImage2('Call two');
   {
   var img = document.getElementById("image");
   img.src="images/tumblr_dash.gif";
   document.getElementById("action").innerHTML = "You have decided to start eating in a minute";
   }
}
function three1() {
   changeImage3('Call three');
   {
   var img = document.getElementById("image");
   img.src="images/eat.gif";
   document.getElementById("action").innerHTML = "You are currently eating";
   }
}

function call1(){
  callCount1++;
  var btn = document.getElementById('eat');
   if(callCount1 == 1) one();
   else if(callCount1 == 2) two();
  else if(callCount1 == 3) three();
  else btn.disabled = true;

  //callOne = false /*!callOne;*/
}

It performs the same functions as the first button, instead of doing what I'm asking it to. 它执行与第一个按钮相同的功能,而不是执行我要的操作。 I've tried changing the numbers of the ChangeImage function but it does nothingHow do I fix this? 我已经尝试过更改ChangeImage函数的编号,但是它什么也没做。如何解决此问题?

Function call1() calls one() two() and three() instead of one1() two1() and three1() . 函数call1()调用one() two()three()而不是one1() two1()three1() That may be the problem. 那可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM