简体   繁体   English

简单的按钮和Javascript功能

[英]Simple buttons and Javascript function

I'm trying to have some buttons and a message displaying depending on the user choice, right now only "correct" shows no matter the choice, what's wrong with my code?: 我试图根据用户的选择显示一些按钮和一条消息,现在无论选择如何,只有“正确”显示,我的代码有什么问题?:

HTML: HTML:

  <button id = "dec12" onclick="clickF1()"> December 12 <br> 2014 </button>
  <button id = "nov13" onclick="clickF1()"> November 13 <br> 2014 </button>
  <button id = "dec15" onclick="clickF1()"> December 15 <br> 2014 </button>

  <h2 id ="output1"></h2>

Javascript: Javascript:

function clickF1() {

var dec12 = document.getElementById("dec12");
var nov13 = document.getElementById("nov13");
var dec15 = document.getElementById("dec15");

if ("dec12") {
  document.getElementById("output1").innerHTML = "Correct!";

} else if ("nov13") {
   document.getElementById("output1").innerHTML = "Way off!";

} else if ("dec15") {
   document.getElementById("output1").innerHTML = "Close but nope!";

 }
}

Your if statements are always true, because a literal string always exists. 您的if语句始终为 true,因为文字字符串始终存在。 (It is always "truthy" in JavaScript.) Even if you remove the quotes and just check the variables, they'd still always be true because those variables always exist. (在JavaScript中,这始终是“真实的”。)即使您删除引号并仅检查变量,它们也始终是真实的,因为这些变量始终存在。

It sounds like you just want to know which element was clicked. 听起来您只是想知道单击了哪个元素。 Have the element pass an identifier into the function. 让元素将标识符传递给函数。 Something like this: 像这样:

<button id = "dec12" onclick="clickF1('dec12')"> December 12 <br> 2014 </button>
<button id = "nov13" onclick="clickF1('nov13')"> November 13 <br> 2014 </button>
<button id = "dec15" onclick="clickF1('dec15')"> December 15 <br> 2014 </button>

Then check that value in the function: 然后在函数中检查该值:

function clickF1(day) {

  if (day == "dec12") {
    document.getElementById("output1").innerHTML = "Correct!";
  } else if (day == "nov13") {
    document.getElementById("output1").innerHTML = "Way off!";
  } else if (day == "dec15") {
    document.getElementById("output1").innerHTML = "Close but nope!";
  }

}

Your clickF1 function is always running the same code. 您的clickF1函数始终运行相同的代码。 You should separate buttons by passing some variable when clicked. 单击时应传递一些变量来分隔按钮。

For example, You can pass button, and then analyze it's id, like this: 例如,您可以传递按钮,然后分析其ID,如下所示:

HTML: HTML:

  <button id = "dec12" onclick="clickF1(this)"> December 12 <br> 2014 </button>
  <button id = "nov13" onclick="clickF1(this)"> November 13 <br> 2014 </button>
  <button id = "dec15" onclick="clickF1(this)"> December 15 <br> 2014 </button>

Javascript: Javascript:

function clickF1(button) {
    var id = button.id;

if (id == "dec12") {
  document.getElementById("output1").innerHTML = "Correct!";

} else if (id == "nov13") {
   document.getElementById("output1").innerHTML = "Way off!";

} else if (id == "dec15") {
   document.getElementById("output1").innerHTML = "Close but nope!";

 }
}

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

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