gpt4 book ai didi

javascript - 输入框计算器不更改结果值

转载 作者:行者123 更新时间:2023-12-02 22:24:49 25 4
gpt4 key购买 nike

我需要制作一个带有输入框的计算器。

这是我的代码,由于某种原因它不会更改结果框值!

如你所见,我尝试了 .val("result").attr("value","re​​sult")两者都不起作用。

一些帮助真的很好!

<html>
<head>
<meta charset="utf-8" />
<title>Praktikum Webanwendungen</title>
<style>
input,
button {
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
$(document).ready(function(){
$("#b1").click(function(){
Add();
});
$("#b2").click(function(){
Substract();
});
$("#b3").click(function(){
Multiply();
});
$("#b4").click(function(){
Divide();
});
});
function Add(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1+no2;
$("#ergebnis").attr("value","result")
}
function Substract(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1-no2;
$("#ergebnis").val(result)
}
function Multiply(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1*no2;
$("#ergebnis").val("result")
}
function Divide(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1/no2;
$("#ergebnis").val("result")
}
</script>
</head>
<body>

<h1>Taschenrechner</h1>
<label for="num1">Zahl1: </label>
<input id="number1" name="num1" type="number" />

<label for="num2">Zahl2: </label>
<input id="number2" name="num2" type="number" />

<div>
<button id="b1">+</button>
<button id="b2">-</button>
<button id="b3">x</button>
<button id="b4">/</button>
</div>

<label for="res">Ergebnis: </label>
<input id="ergebnis" name="res" type="number" value = "" >
</body>
</html>

最佳答案

您忘记关闭脚本标记。我已经更新了您的代码,请检查

<html>
<head>
<meta charset="utf-8" />
<title>Praktikum Webanwendungen</title>
<style>
input,
button {
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#b1").on('click',function(){
Add();
});
$("#b2").on('click',function(){
Substract();
});
$("#b3").on('click',function(){
Multiply();
});
$("#b4").on('click',function(){
Divide();
});
});
function Add(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1+no2;
$("#ergebnis").val(result)
}
function Substract(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1-no2;
$("#ergebnis").val(result)
}
function Multiply(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1*no2;
$("#ergebnis").val(result)
}
function Divide(){
var no1=parseInt($("#number1").val());
var no2=parseInt($("#number2").val());
var result =no1/no2;
$("#ergebnis").val(result)
}
</script>
</head>
<body>

<h1>Taschenrechner</h1>
<label for="num1">Zahl1: </label>
<input id="number1" name="num1" type="number" />

<label for="num2">Zahl2: </label>
<input id="number2" name="num2" type="number" />

<div>
<button id="b1">+</button>
<button id="b2">-</button>
<button id="b3">x</button>
<button id="b4">/</button>
</div>

<label for="res">Ergebnis: </label>
<input id="ergebnis" name="res" type="number" value = "" >
</body>
</html>

关于javascript - 输入框计算器不更改结果值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com