Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用HTML和javascript制作在线计算器_Javascript_Html_Forms_Var_Equation - Fatal编程技术网

用HTML和javascript制作在线计算器

用HTML和javascript制作在线计算器,javascript,html,forms,var,equation,Javascript,Html,Forms,Var,Equation,我想制作一个在线计算器,我想,我可以使用HTML中的表单,我制作了一些表单,但没有什么问题,我需要从(input type=“number”name=“first number”)和(input type=“number”name=“second number”)中获取值,然后将它们放入脚本等式中,然后将结果写入(input type=“text”)当用户输入文件编号并按下提交按钮时 卡尔库拉契卡 扎德吉·兹德·普夫尼西斯洛 + - x : 扎德吉·兹德·德鲁赫西斯洛 výsledek: 变

我想制作一个在线计算器,我想,我可以使用HTML中的表单,我制作了一些表单,但没有什么问题,我需要从(input type=“number”name=“first number”)和(input type=“number”name=“second number”)中获取值,然后将它们放入脚本等式中,然后将结果写入(input type=“text”)当用户输入文件编号并按下提交按钮时


卡尔库拉契卡
扎德吉·兹德·普夫尼西斯洛
+
-
x
:
扎德吉·兹德·德鲁赫西斯洛
výsledek:
变量a,文本;
y=
x=
a=y+x
文本=+a;
文件。书写(文本);
变量b,文本;
y=
x=
b=y-x
文本=+b;
文件。书写(文本);
变量c,文本;
y=
x=
c=y*x
text=+c;
文件。书写(文本);
变量d,文本;
y=
x=
d=y/x
text=+d;
文件。书写(文本);

这里有一个小示例,说明如何使用
document.getElementById()
输入
元素读取,并使用
按钮中的
onclick
事件处理程序写入
span
元素

A:
B:
算计

A*B=
我真的不明白你的问题,也不明白你想做什么

但是,要获取输入字段的数据,需要执行DOM访问:

document.getElementsByTagName('vysledek')[0].value = "test1234";
您需要通过以下方式回复:

<code>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    width: 130px;
}

#CLEAR {
    width: 150px;
}

button {
    width: 45px;
}
</style>
<script>
function myFunction(clickedId) {
    document.calc.result.value+=clickedId;
}
function Clear() {
    document.calc.result.value="";
}
function compute() {
    try{
    var inp=eval(document.calc.result.value);
    document.calc.result.value=inp;
    }catch(err){
            document.calc.result.value="Bad Input!!";
    }
}
</script>
</head>
<body>
    <form name="calc">
    <input type="text" name="result" readonly>
    </form>

<table >
  <tr>
    <td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td>
    <td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td>
    <td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td>
  </tr>
  <tr>
    <td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td>
    <td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td>
    <td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td>
  </tr>
  <tr>
    <td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td>
    <td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td>
    <td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td>
  </tr>
  <tr>
    <td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td>
    <td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td>
    <td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td>
  </tr>
  <tr>
    <td><button type="button" id="*" onclick="myFunction(this.id)">*</button></td>
    <td><button type="button" id="/" onclick="myFunction(this.id)">/</button></td>
    <td><button type="button" id="ANS" onclick="compute()">ANS</button></td>
  </tr>
</table>

<button type="button" id="CLEAR" onclick="Clear()">CLEAR</button>

</body></html>


</code>
也许你应该先读一本JavaScriptDOM教程。比如这个

致意


Dustin

我开发了一个简单的计算器,它使用Java Scrip eval。这种方法可能会对您有所帮助

你可以在这里在线测试


表,th,td{
边框:1px纯黑;
宽度:130px;
}
#清楚的{
宽度:150px;
}
钮扣{
宽度:45px;
}
函数myFunction(单击EDID){
document.calc.result.value+=点击edid;
}
函数Clear(){
document.calc.result.value=“”;
}
函数compute(){
试一试{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}捕捉(错误){
document.calc.result.value=“输入错误!!”;
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9
0
+
-
*
/
ANS
清楚的

您是否应该使用
数据-
属性而不是
id
属性来确定按钮的值(在这种情况下,我甚至可以看到正在使用的元素的值,即
this.innerText
)?我认为使用
id
属性来实现这一点不是一个好主意,这是一个很好的观点。我会试试的。我对javascript还不熟悉,所以我只是想让它工作起来。
document.getElementsByTagName('vysledek')[0].value = "test1234";
<code>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    width: 130px;
}

#CLEAR {
    width: 150px;
}

button {
    width: 45px;
}
</style>
<script>
function myFunction(clickedId) {
    document.calc.result.value+=clickedId;
}
function Clear() {
    document.calc.result.value="";
}
function compute() {
    try{
    var inp=eval(document.calc.result.value);
    document.calc.result.value=inp;
    }catch(err){
            document.calc.result.value="Bad Input!!";
    }
}
</script>
</head>
<body>
    <form name="calc">
    <input type="text" name="result" readonly>
    </form>

<table >
  <tr>
    <td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td>
    <td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td>
    <td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td>
  </tr>
  <tr>
    <td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td>
    <td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td>
    <td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td>
  </tr>
  <tr>
    <td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td>
    <td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td>
    <td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td>
  </tr>
  <tr>
    <td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td>
    <td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td>
    <td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td>
  </tr>
  <tr>
    <td><button type="button" id="*" onclick="myFunction(this.id)">*</button></td>
    <td><button type="button" id="/" onclick="myFunction(this.id)">/</button></td>
    <td><button type="button" id="ANS" onclick="compute()">ANS</button></td>
  </tr>
</table>

<button type="button" id="CLEAR" onclick="Clear()">CLEAR</button>

</body></html>


</code>