我做了一个化妆品计算器,它的功能很好,但我想添加一个功能,当我按回车键时,它会运行计算。现在在程序中我必须手动点击“rechnen”按钮来计算。有什么想法,我可以做什么,而不是总是用鼠标来计算?
我试过使用W3schools的一个例子,并将代码实现到我的代码中,但它似乎不起作用。
var Waschlotion = 1.30;
var Haarshampoo = 1.50;
var Bodylotion = 3;
var Wundschutzcreme = 4.80;
var Deospray = 1.80;
var Zahncreme = 1.50;
var Zahnbuerste = 1;
var Zahnspuellbecher = 1.25;
var Zahnhaftcreme = 3.20;
var Zahnreinigunstabs = 3.20;
var Rasierklingen = 2.49;
var RasiererMitKlingen = 4.50;
var Rasierschaum = 1.40;
var Einwegrasierer = 2;
var ZahnreinigunsBecher = 5;
var Mullkompressen = 6;
let Sum;
let WaschlotionAmount;
let HaarshampooAmount;
let BodylotionAmount;
let WundschutzcremeAmount;
let DeosprayAmount;
let ZahncremeAmount;
let ZahnbuersteAmount;
let ZahnspuellbecherAmount;
let ZahnhaftcremeAmount;
let ZahnreinigunstabsAmount;
let RasierklingenAmount;
let RasiererMitKlingenAmount;
let RasierschaumAmount;
let EinwegrasiererAmount;
let ZahnreinigunsBecherAmount;
let MullkompressenAmount;
function Calculate() {
if (Sum != 0) {
Sum = 0;
}
let WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
let HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
let BodylotionAmount = document.getElementById("BodylotionAmount").value;
let WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
let DeosprayAmount = document.getElementById("DeosprayAmount").value;
let ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
let ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
let ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
let ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
let ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
let RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
let RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
let RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
let EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
let ZahnreinigunsBecherAmount = document.getElementById("ZahnreinigunsBecherAmount").value;
let MullkompressenAmount = document.getElementById("MullkompressenAmount").value;
Sum = (Waschlotion * WaschlotionAmount) + (Haarshampoo * HaarshampooAmount) + (Bodylotion * BodylotionAmount) +
(Wundschutzcreme * WundschutzcremeAmount) + (Deospray * DeosprayAmount) + (Zahncreme * ZahncremeAmount) +
(Zahnbuerste * ZahnbuersteAmount) + (Zahnspuellbecher * ZahnspuellbecherAmount) + (Zahnhaftcreme * ZahnhaftcremeAmount) +
(Zahnreinigunstabs * ZahnreinigunstabsAmount) + (Rasierklingen * RasierklingenAmount) + (RasiererMitKlingen * RasiererMitKlingenAmount) +
(Rasierschaum * RasierschaumAmount) + (Einwegrasierer * EinwegrasiererAmount) + (ZahnreinigunsBecher * ZahnreinigunsBecherAmount) + (Mullkompressen * MullkompressenAmount);
var roundedString = Sum.toFixed(2);
var rounded = Number(roundedString);
document.getElementById("Sum").innerHTML = rounded;
var input = document.getElementById("Rechnen");
Rechnen.addEventListener("keypress",
function(event) {
if (event.key === "Enter") {
event.preventDefault();
document.getElementById("Rechnen").click();
}
});
}
function ClearAll() {
document.getElementById("WaschlotionAmount").value = "";
document.getElementById("HaarshampooAmount").value = "";
document.getElementById("BodylotionAmount").value = "";
document.getElementById("WundschutzcremeAmount").value = "";
document.getElementById("DeosprayAmount").value = "";
document.getElementById("ZahncremeAmount").value = "";
document.getElementById("ZahnbuersteAmount").value = "";
document.getElementById("ZahnspuellbecherAmount").value = "";
document.getElementById("ZahnhaftcremeAmount").value = "";
document.getElementById("ZahnreinigunstabsAmount").value = "";
document.getElementById("RasierklingenAmount").value = "";
document.getElementById("RasiererMitKlingenAmount").value = "";
document.getElementById("RasierschaumAmount").value = "";
document.getElementById("EinwegrasiererAmount").value = "";
document.getElementById("MullkompressenAmount").value = "";
document.getElementById("ZahnreinigunsBecherAmount").value = "";
}
td {
text-align: center;
font-weight: bold;
}
table {
text-align: center;
}
.center {
margin-top: 20px;
margin-left: auto;
margin-right: auto;
}
.buttons {
margin-top: 20px;
margin-left: 100px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
button {
cursor: pointer;
}
#Rechnen {
margin-right: 10px;
}
<table class="center">
<tr>
<th>Kosmetikartikel</th>
<th>Menge</th>
</tr>
<tr>
<td>Waschlotion</td>
<td><input id="WaschlotionAmount"></td>
<td> 1,30 </td>
</tr>
<tr>
<td>Haarshampoo</td>
<td><input id="HaarshampooAmount"></td>
<td>1,50</td>
</tr>
<tr>
<td>Bodylotion</td>
<td><input id="BodylotionAmount"></td>
<td>3</td>
</tr>
<tr>
<td>Wundschutzcreme</td>
<td><input id="WundschutzcremeAmount"></td>
<td>4,80</td>
</tr>
<tr>
<td>Deospray</td>
<td><input id="DeosprayAmount"></td>
<td>1,80</td>
</tr>
<tr>
<td>Zahncreme</td>
<td><input id="ZahncremeAmount"></td>
<td>1,50</td>
</tr>
<tr>
<td>Zahnbürste</td>
<td><input id="ZahnbuersteAmount"></td>
<td>1</td>
</tr>
<tr>
<td>Zahnspüllbecher</td>
<td><input id="ZahnspuellbecherAmount"></td>
<td>1,25</td>
</tr>
<tr>
<td>Zahnhaftcreme</td>
<td><input id="ZahnhaftcremeAmount"></td>
<td>3,20</td>
</tr>
<tr>
<td>Zahnreinigunstabs</td>
<td><input id="ZahnreinigunstabsAmount"></td>
<td>3,20</td>
</tr>
<tr>
<td>Rasierklingen</td>
<td><input id="RasierklingenAmount"></td>
<td>2,49</td>
</tr>
<tr>
<td>Rasierer mit Klingen</td>
<td><input id="RasiererMitKlingenAmount"></td>
<td>4.50</td>
</tr>
<tr>
<td>Rasierschaum</td>
<td><input id="RasierschaumAmount"></td>
<td>1,40</td>
</tr>
<tr>
<td>Einwegrasierer</td>
<td><input id="EinwegrasiererAmount"></td>
<td>2</td>
</tr>
<tr>
<td>Zahnreinigunsbecher</td>
<td><input id="ZahnreinigunsBecherAmount"></td>
<td>5</td>
</tr>
<tr>
<td>Mullkompressen</td>
<td><input id="MullkompressenAmount"></td>
<td>6</td>
</tr>
<tr>
<td><b>Gesamtpreis</b></td>
<td><b id="Sum"></b></td>
</tr>
</table>
<div class="buttons">
<button id="Rechnen" onclick="Calculate()">Rechnen</button>
<button id="Clear All" onclick="ClearAll()">Clear All</button>
</div>
4条答案
按热度按时间ws51t4hk1#
nwsw7zdq2#
你犯了一个错误。您正在将按键事件与提交按钮绑定。您需要与文件绑定。第二,只需要将该代码移出计算函数。它会工作得很好。下面是更新后的代码。
iq0todco3#
在按键事件中添加一个EventListener,在回车键时调用`Calculate'函数。
阅读更多关于JS中EventListeners的信息:W3 School about EventListener
以下是一个示例解决方案:
lmvvr0a84#
我会简化这一点,并使用一个提交事件的表单。提交将计算,因此将输入任何东西。
我添加了一个重置,并将数量字段更改为type=“number”
我在用