我想做一些类似的事情,以前做过很多,只是稍微扭曲一下。我想创建2个输入字段,在我写下我需要写的内容后,按下计算按钮找到计算结果。我能够+, - ,*和/。问题是,我能够制作一个代码,当我按下这些符号时,答案就会到来。但是,当我点击符号而不是计算按钮时,我不希望得到答案呢?我怎么做?请不要编写与我完全不同的代码。如果你不知道怎么做,请不要帮忙(虽然我很感激你想帮忙)。这里有些东西我不明白。请告诉我我做错了什么以及为什么。这是我的代码。
function kalkuler() {
document.getElementById('pluss').onclick;
document.getElementById('minus').onclick;
document.getElementById('gange').onclick;
document.getElementById('dele').onclick;
document.getElementById('btnKnapp').onclick;
var boks1 = parseInt(document.getElementById('txtBoks1').value);
var boks2 = parseInt(document.getElementById('txtBoks1').value);
document.getElementById('utskrift').innerHTML;
function pluss() {
document.getElementById('utskrift').value = boks1 + boks2;
}
function minus() {
document.getElementById('utskrift').value = boks1 - boks2;
}
function gange() {
document.getElementById('utskrift').value = boks1 * boks2;
}
function dele() {
document.getElementById('utskrift').value = boks1 / boks2;
}
}

Tall 1:<input id="txtBoks1" type="text" name="txtBoks1" />
<br><br>
<select name="regnetegn">
<option id="pluss" value="pluss" name="+">+</option>
<option id="minus" value="minus" name="-">-</option>
<option id="gange" value="gange" name="*">*</option>
<option id="dele" value="deling" name="/">/</option>
</select>
<br><br>
Tall 2:<input id="txtBoks2" type="text" name="txtBoks2" />
<br><br>
<button onclick="kalkuler" id="btnKnapp" type="button">Kalkuler</button>
<p id="utskrift"></p>
&#13;
答案 0 :(得分:1)
试试这个。你可以从select中取值。然后改变
var boks2 = parseInt(document.getElementById('txtBoks1').value);
到
var boks2 = parseInt(document.getElementById('txtBoks2').value);
同时将document.getElementById('utskrift').value
更改为document.getElementById('utskrift').innerHTML
function kalkuler() {
var calcOperation = document.getElementById('calcOption').value;
/*Remove this
document.getElementById('pluss').onclick;
document.getElementById('minus').onclick;
document.getElementById('gange').onclick;
document.getElementById('dele').onclick;
document.getElementById('btnKnapp').onclick;*/
var boks1 = parseInt(document.getElementById('txtBoks1').value);
var boks2 = parseInt(document.getElementById('txtBoks2').value);
document.getElementById('utskrift').innerHTML;
if (calcOperation == 'pluss') {
document.getElementById('utskrift').innerHTML = boks1 + boks2;
}
if (calcOperation == 'minus') {
document.getElementById('utskrift').innerHTML = boks1 - boks2;
}
if (calcOperation == 'gange') {
document.getElementById('utskrift').innerHTML = boks1 * boks2;
}
if (calcOperation == 'deling') {
document.getElementById('utskrift').innerHTML = boks1 / boks2;
}
}
<label>
Tall 1:
<input id="txtBoks1" type="number" name="txtBoks1" />
</label>
</br>
</br>
<select name="regnetegn" id="calcOption">
<option value="pluss" name="+">+</option>
<option value="minus" name="-">-</option>
<option value="gange" name="*">*</option>
<option value="deling" name="/">/</option>
</select>
</br>
</br>
<label>
Tall 2:
<input id="txtBoks2" type="number" name="txtBoks2" />
</label>
</br>
</br>
<button onclick="kalkuler()" id="btnKnapp" type="button">Kalkuler</button>
<p id="utskrift"></p>
答案 1 :(得分:0)
请使用以下代码
Tall 1:<input id="txtBoks1" type="text" name="txtBoks1" />
<br><br>
<select name="regnetegn" id="regnetegn">
<option id="pluss" value="pluss" name="+">+</option>
<option id="minus" value="minus" name="-">-</option>
<option id="gange" value="gange" name="*">*</option>
<option id="dele" value="deling" name="/">/</option>
</select>
<br><br>
Tall 2:<input id="txtBoks2" type="text" name="txtBoks2" />
<br><br>
<button onclick="kalkuler();" id="btnKnapp" type="button">Kalkuler</button>
<p id="utskrift"></p>
<script>
function kalkuler() {
var boks1 = parseInt(document.getElementById('txtBoks1').value);
var boks2 = parseInt(document.getElementById('txtBoks2').value);
var regnetegn = document.getElementById('regnetegn').value;
document.getElementById('utskrift').innerHTML = '';
if(regnetegn == "pluss")
pluss(boks1,boks2);
if(regnetegn == "minus")
minus(boks1,boks2);
if(regnetegn == "gange")
gange(boks1,boks2);
if(regnetegn == "deling")
dele(boks1,boks2);
return false;
}
function pluss(boks1,boks2) {
document.getElementById('utskrift').innerHTML = boks1 + boks2;
}
function minus(boks1,boks2) {
document.getElementById('utskrift').innerHTML = boks1 - boks2;
}
function gange(boks1,boks2) {
document.getElementById('utskrift').innerHTML = boks1 * boks2;
}
function dele(boks1,boks2) {
document.getElementById('utskrift').innerHTML = boks1 / boks2;
}
</script>