[英]Javascript beginner - Can anyone tell me why my button doesn't work?
jsfiddle在這里-> http://jsfiddle.net/diabetesjones/015k1tjn/5/
這是一個有效的示例: http : //jsfiddle.net/Ahopefulmachine/dkhj8o38/
關於第二個jsfiddle(不是我的),我不太明白為什么他的工作方式不使用按鈕上的document.getElementById。
我覺得我的問題出在點擊功能本身上:
document.getElementById('mainButton').onclick = function () {
謝謝 :)
現在正在工作: http : //jsfiddle.net/doniyor/015k1tjn/9/
您有document.getElementByID
應該是document.getElementById
和
numberOne = parseInt(numberOne, 10);
那樣將字符串轉換為int numberOne = parseInt(numberOne, 10);
其中10
是小數的基數。 question == ADD
應該是question == 'ADD'
您的代碼存在三件事:
getElementById
而不是getElementByID
(區分大小寫)。 document.getElementById('number1').value;
前面添加一個+
來輕松實現此目的document.getElementById('number1').value;
question == ADD
而不是question == 'ADD'
請參閱更正的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.