【问题标题】:How to make several buttons with call functions all work如何使多个具有呼叫功能的按钮全部工作
【发布时间】:2018-03-08 15:51:38
【问题描述】:

我试图拥有多个按钮,每个按钮都有一个单独的调用功能,但要么所有按钮都做同样的事情,要么根本不工作。 所以这是我的第一个按钮:

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

这是它的脚本:

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;*/
}

而且效果很好。但是当我按下第二个按钮时:

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

哪个有这个脚本:

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;*/
}

它执行与第一个按钮相同的功能,而不是按照我的要求执行。我已尝试更改 ChangeImage 函数的数字,但它什么也没做我该如何解决这个问题?

【问题讨论】:

    标签: javascript html button call


    【解决方案1】:

    函数 call1() 调用 one() two()three() 而不是 one1() two1()three1()。这可能是问题所在。

    【讨论】:

    • 好的,关于如何重命名它们来解决这个问题有什么建议吗?
    • 等等我想通了。非常感谢!
    • 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多