简体   繁体   English

无法设置按钮的onclick属性

[英]Unable to set the onclick property of a button

I have a button calling a js function. 我有一个调用js函数的按钮。 Everything in the function runs but at the end I try to set the onclick value for the button and nothing happens. 函数中的所有内容都会运行,但最后我尝试为按钮设置onclick值,但没有任何反应。 The catch block doesn't even generate an error catch块甚至不会产生错误

var nextButton = document.getElementById("next");
try{
if(tvalue == "trend"){
  nextButton.onclick = "generateLocations()";
}
else if (tvalue == "stats"){
  nextButton.onclick = "generateCategories()";
}
}
catch(err) {
  document.getElementById("demo").innerHTML = err.message;
} 

I have also tried setting the onclick value these ways 我也尝试过通过以下方式设置onclick值

nextButton.onclick = function () { generateCategories(); };
nextButton.onclick = generateCategories;

the tvalue variable is set like this: tvalue变量设置如下:

var t = document.getElementById("type");
var tvalue = t.options[t.selectedIndex].value;

Other test outputs have shown that this retrieves the value no problem 其他测试输出表明,这可以检索出值,没问题

The relevant HTML: 相关的HTML:

<form id='fieldsets' action="../scripts/reportParse.php">
<fieldset>
<legend> Select Report Type </legend>
  <select id="type" name="type">
    <option value="trend"> Waitlists over Time </option>
    <option value="stats"> Region Statistics </option>
  </select>
</fieldset>

<button id="next" onclick="generateRegions()"> Next </button>

I've been trying solutions based on the following page: 我一直在尝试基于以下页面的解决方案:

Change onclick action with a Javascript function 使用Javascript函数更改onclick操作

and w3schools.com 和w3schools.com

Thanks for any help. 谢谢你的帮助。 I've been hung up on this for a little bit now 我已经为此挂了一点

Edit: I've tried useing event listeners based on a comment below and am now getting the error "i is not defined'. which seems odd since i is not referred too inside the try block. 编辑:我已经尝试根据下面的评论使用事件侦听器,现在收到错误“ i is not defined”。这似乎很奇怪,因为在try块中也没有引用我。

The whole function in case i've missed something stupid: 整个功能,以防我错过了一些愚蠢的事情:

function generateRegions(){
var t = document.getElementById("type");
var tvalue = t.options[t.selectedIndex].value; // The value of the selected option

var names = ["Downtown", "Glenmore", "Mission", "Rutland"];

var regions = document.createElement("FIELDSET");
regions.setAttribute("id","Region");
var temp = document.createElement("LEGEND");
temp.innerHTML = "Select Region:";
regions.appendChild(temp); //creating the fieldset div and assigning its legend

for(var i = 0; i < 4; i++){
  var templ = document.createElement("LABEL");

  var str1 = "chk";
  var str2 = i.toString();
  var id = str1.concat(str2); //creating a dynamic ID so each checkbox can be referred to individually

  templ.setAttribute("for",id);

  temp = document.createElement("INPUT"); //creating the checkbox and assigning its values

  temp.setAttribute("type","checkbox");
  temp.setAttribute("name","region");
  temp.setAttribute("value",names[i]);

  temp.setAttribute("id",id);

  regions.appendChild(templ);
  templ.innerText = names[i]+':'; //creating and placing the label, then placing its checkbox
  regions.appendChild(temp);
}

document.getElementById("fieldsets").appendChild(regions); //adding the fieldset to the overall form
var nextButton = document.getElementById("next");
try{
if(tvalue == "trend"){
  nextButton.onclick = "generateLocations()";
}
else if (tvalue == "stats"){
  nextButton.onclick = "generateCategories()";
}
}
catch(err) {
  document.getElementById("demo").innerHTML = err.message;
} 

//document.getElementById("demo").innerHTML = tvalue; //checking that the type variable is properly found

} }

Try adding an event listener to your code so the JavaScript is separated from the HTML markup. 尝试将事件侦听器添加到代码中,以使JavaScript与HTML标记分离。 Like so: 像这样:

var nextButton = document.getElementById("next");
try{
if(tvalue == "trend"){
  nextButton.addEventListener("click",generateLocations()) ;
}
else if (tvalue == "stats"){
  nextButton.addEventListener("click",generaCategories()) ;
}
}
catch(err) {
  document.getElementById("demo").innerHTML = err.message;
} 

You can test if the event listener is working by adding a console log on the functions you are executing. 您可以通过在正在执行的功能上添加控制台日志来测试事件侦听器是否正常工作。

Source: https://www.w3schools.com/js/js_htmldom_eventlistener.asp 资料来源: https : //www.w3schools.com/js/js_htmldom_eventlistener.asp

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

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