[英]JavaScript, output in calculator
I have a problem with my very simple calculator in JS. 我在JS中使用非常简单的计算器时遇到问题。 I want it to show the result in a box but I don't know what it is that I'm doing wrong.
我希望它在框中显示结果,但是我不知道我做错了什么。 There is probably some more things in my code that might not be right... Please help me!
我的代码中可能还有其他不正确的地方,请帮助我!
Javascript: Javascript:
document.getElementById('submit').onclick=function() { num1 = parseFloat(document.getElementById('valueA').value); num2 = parseFloat(document.getElementById('valueB').value); arithmeticOperator = document.getElementById('arithmeticOperator').value; switch(arithmeticOperator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if(num2 != 0) { result = num1 / num2; }else { result = 'Dela inte med 0!'; } break; default: result = 'Error'; } document.getElementById('calcForm').onclick=output; window.alert('result'); };
<!DOCTYPE html> <html> <head> <title>blabla</title> <meta charset="utf-8"/> <script src="js.js" defer></script> </head> <body> <form name="calcForm" id="calcForm"> <p><input type="number" name="valueA" placeholder="Värde A" /></p> <p><input type="number" name="valueB" placeholder="Värde B" /></p> <select name="arithmeticOperator"> <option value="+">Addition</option> <option value="-">Subtraktion</option> <option value="/">Division</option> <option value="*">Multiplication</option> </select> <button type="submit">Räkna ut</button> <p><output name="result" form="calcForm"></output></p> </form> </body> </html>
:) :)
You are selecting by id
, but the value arithmeticOperator
is the name
of your select box. 您正在按
id
进行选择,但值arithmeticOperator
是选择框的name
。
Try this: <select name="arithmeticOperator" id="arithmeticOperator">
尝试以下操作:
<select name="arithmeticOperator" id="arithmeticOperator">
document.getElementById
gets elements by their id
. document.getElementById
通过其id
获取元素。 You have multiple cases where you're trying to find elements with their name or type. 在多种情况下,您尝试查找具有其名称或类型的元素。 That won't work.
那行不通。
document.getElementById('calcForm').onclick=output;
This tries to bind a function called output to the click event of an element called calcForm. 这试图将称为输出的函数绑定到名为calcForm的元素的click事件。 I have no idea what you're trying to do here.
我不知道你要在这里做什么。
To show the result in an input or output, you can just do 要在输入或输出中显示结果,您可以
document.getElementById("idOfTheElement").value = result;
As you seem eager to just know the solution - I've created this fiddle for you. 您似乎很想知道解决方案-我为您创建了这个小提琴 。
Please go over it carefully and check the edits I've done in your code. 请仔细检查并检查我在您的代码中所做的修改。
Numerous errors, typos changed (as mentioned by others) include: 许多错误,错别字已更改(如其他人所述)包括:
name=
instance can be swapped out by id=
in your code.. It's crucial in javascript to understand the difference. name=
实例都可以通过代码中的id=
换出。.了解这些区别在javascript中至关重要。 var
(including arrays, objects and often even functions) var
开头(包括数组,对象甚至是函数) event.preventDefault()
event.preventDefault()
的第一行。 The javascript code reworked: javascript代码重做:
document.getElementById('submitButton').onclick = function(event) {
event.preventDefault();
var num1 = parseFloat(document.getElementById('valueA').value),
num2 = parseFloat(document.getElementById('valueB').value),
arithmeticOperator = document.getElementById('arithmeticOperator').value,
output = document.getElementById('result');
switch(arithmeticOperator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
{
result = num1 / num2;
}else {
result = 'Dela inte med 0!';
}
break;
default:
result = 'Error';
}
output.innerHTML = result;
window.alert(result);
return false;
};
And the HTML with some modifications: 以及带有一些修改的HTML:
<form name="calcForm" id="calcForm">
<p><input type="number" id="valueA" placeholder="Värde A" /></p>
<p><input type="number" id="valueB" placeholder="Värde B" /></p>
<select name="arithmeticOperator" id="arithmeticOperator">
<option value="+">Addition</option>
<option value="-">Subtraktion</option>
<option value="/">Division</option>
<option value="*">Multiplication</option>
</select>
<button type="submit" id="submitButton">Räkna ut</button>
<p>Result: <output id="result" form="calcForm"></output></p>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.