Javascript不等待onclick

时间:2015-05-04 10:59:01

标签: javascript html

我正在尝试执行此代码但由于某种原因我无法弄清楚RoundRobin函数将执行onload而不是onclick。我试过删除window.onload或用document.onload替换它。我只想让这一行var selectedTeams = []; window.onload = function() { // JavaScript Document // add selected teams to array var teamList = document.getElementById("teamDropdown"); teamList.onchange = function addTeams() { if (selectedTeams.length > 7) { alert("no more teams") } else { var county = document.getElementById("teamDropdown").value; selectedTeams.push(county); } alert("counties selected are: " + selectedTeams); } var teamsIn = 0; teamsIn = selectedTeams.length; document.getElementById("tableButton").onclick = RoundRobin(teamsIn); // slideshow. var imageArray = new Array(); imageArray[0] = new Image() imageArray[0].src = "Sponsors/aib.png" imageArray[1] = new Image() imageArray[1].src = "Sponsors/centra.jpg" imageArray[2] = new Image() imageArray[2].src = "Sponsors/eircom.png" imageArray[3] = new Image() imageArray[3].src = "Sponsors/etihad.png" imageArray[4] = new Image() imageArray[4].src = "Sponsors/liberty.jpg" imageArray[5] = new Image() imageArray[5].src = "Sponsors/supervalu.png" var step = 0; function slideShow() { document.getElementById('slideshow').src = imageArray[step].src if (step < 5) step++ else step = 0 setTimeout("slideShow()", 4000) } function RoundRobin(teams) { alert(teams); var i; var ret = ""; var round; var numplayers = 0; numplayers = parseInt(teams) + parseInt(teams % 2); numplayers = parseInt(numplayers); alert(numplayers); var a = new Array(numplayers - 1); var alength = a.length; for (var x = 0; x < (numplayers); x++) { a[x] = "Team " + (x + 1); } if (numplayers != parseInt(teams)) { a[alength] = "BYE"; } var pos; var pos2; ret = "----- ROUND #1-----<br />" for (var r1a = 0; r1a < (numplayers / 2); r1a++) { ret += a[r1a] + " vs. " + a[alength - r1a] + "<br />" } for (round = 2; round < alength + 1; round++) { ret += "<br /><br />----- ROUND #" + round + "-----<br />" ret += a[0] + " vs. " + a[alength - (round - 1)] + "<br />" for (i = 2; i < (numplayers / 2) + 1; i++) { pos = (i + (round - 2)) if (pos >= alength) { pos = ((alength - pos)) * -1 } else { pos = (i + (round - 2)) } pos2 = (pos - (round - 2)) - round if (pos2 > 0) { pos2 = (alength - pos2) * -1 } if (pos2 < (alength * -1)) { pos2 += alength } ret += a[(alength + pos2)] ret += " vs. " + a[(alength - pos)] + "<br />" } } var text = document.getElementById('fixtures'); text.innerHTML = ret; return ret } // round robin format } 等待onclick触发器。

App::build(array(
  'Controller' => array(
    ROOT . '/app/Controller/Api/'
  )
));

1 个答案:

答案 0 :(得分:6)

发生的事情是你调用函数,然后你将返回值赋给onclick属性。要使事件处理程序起作用,您需要为属性分配一个函数。

在函数表达式中包含调用:

document.getElementById("tableButton").onclick = function(){
  RoundRobin(teamsIn);
};