[英]Javascript beginner - Can anyone tell me why my button doesn't work?
jsfiddle is here --> http://jsfiddle.net/diabetesjones/015k1tjn/5/ jsfiddle在这里-> http://jsfiddle.net/diabetesjones/015k1tjn/5/
here's an example of one that does work: http://jsfiddle.net/Ahopefulmachine/dkhj8o38/ 这是一个有效的示例: http : //jsfiddle.net/Ahopefulmachine/dkhj8o38/
Regarding the second jsfiddle (not mine), I don't quite get why his works without using document.getElementById on the button. 关于第二个jsfiddle(不是我的),我不太明白为什么他的工作方式不使用按钮上的document.getElementById。
i feel like my problem is in the click function itself of : 我觉得我的问题出在点击功能本身上:
document.getElementById('mainButton').onclick = function () {
thanks :) 谢谢 :)
working now: http://jsfiddle.net/doniyor/015k1tjn/9/ 现在正在工作: http : //jsfiddle.net/doniyor/015k1tjn/9/
you had document.getElementByID
which should be document.getElementById
您有document.getElementByID
应该是document.getElementById
AND 和
numberOne = parseInt(numberOne, 10);
您没有像numberOne = parseInt(numberOne, 10);
那样将字符串转换为int numberOne = parseInt(numberOne, 10);
where 10
is radix for decimal. 其中10
是小数的基数。 question == ADD
which should be question == 'ADD'
您有question == ADD
应该是question == 'ADD'
There are three things wrong with your code: 您的代码存在三件事:
getElementById
not getElementByID
(case-sensitive). 是getElementById
而不是getElementByID
(区分大小写)。 +
in front of the document.getElementById('number1').value;
您可以通过在document.getElementById('number1').value;
前面添加一个+
来轻松实现此目的document.getElementById('number1').value;
question == ADD
instead of question == 'ADD'
前question == ADD
而不是question == 'ADD'
See corrected jsFiddle example 请参阅更正的jsFiddle示例
document.getElementById('mainButton').onclick = function () {
//Getting values of inputs and saving them to variables
var numberOne = +document.getElementById('number1').value;
var numberTwo = +document.getElementById('number2').value;
//Setting values of the equations
var addition = (numberOne + numberTwo);
var subtraction = (numberOne - numberTwo);
var multiplication = (numberOne * numberTwo);
var division = (numberOne / numberTwo);
//Prompting user for their desired equation
var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();
//If statement to show the proper equation based on the user's prior prompt
if (question == 'ADD') {
alert('I added the shit out of those numbers for you - turns out it is ' + addition);
} else if (question == 'SUBTRACT') {
alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
} else if (question == 'MULTIPLY') {
alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
} else if (question == 'DIVIDE') {
alert('This ones my favorite, I love a good division - ' + division);
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.