你好,我正在嘗試創建一個腳本,將通過加法或減法來計算總數。我遇到的問題是當前使用的JavaScript,我發現只適用於起始金額字段和一次只有一個賬單金額字段。例如,如果我在開始金額中輸入50,並在賬單金額字段中輸入50,它將計算它。如果我添加一行然後輸入一個金額,它仍然只會根據什麼輸入到賬單金額字段之一來調整金額。協助Calc語言的JavaScript函數
不知道爲什麼,但代碼不能在jsfiddle上運行,但您將能夠看到此代碼在此jsFiddle。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Finance</title>
<meta charset="UTF-8">
<meta name="finance" content="width=device-width, initial-scale=1.0">
<style>
table {
padding-top: 15px;
}
td {
white-space: nowrap;
}
button {
cursor: pointer;
}
</style>
</head>
<body>
<h1>Financial Keeps</h1>
<p><b>Starting Amount: $ <input type="number" id="startAmt" name="startAmt" onkeyup="calc(this)"/></b></p>
<p>To subtract an amount place a minus (-) sign infront of the dollar amount.</p>
<button onclick="insertRow()" id="addRow" >Add Row</button>
<!--<button onclick="removeRow()" id="delRow" >Delete Row</button>-->
<table id="myTable">
<tr>
<th>Bill Info</th>
<th>Bill Amount</th>
<th>Date</th>
<th>Comment</th>
</tr>
<tr>
<tr>
<td><input type="text" id="billInfo"></td>
<td><input type="number" id="billAmt" name="number" onkeyup="calc(this)"></td>
<td><input type="date" id="date"></td>
<td><input type="text" id="commentBox"></td>
<!--<td><input style="cursor: pointer;" type="button" id="delBtn" value="Delete" onclick="removeRow(this)"></td>-->
</tr>
</table>
<input type="hidden" id="total" name="total" value="0" />
<p><b>Ending Amount: $ <span id="totalAmt">0</span></b></p>
<script type="text/javascript">
function insertRow() {
var x = document.getElementById("myTable");
var row = x.insertRow(x.rows.length);
var cell = row.insertCell(0);
var a = document.createElement("input");
a.setAttribute("type","text");
cell.appendChild(a);
var cell1 = row.insertCell(1);
var b = document.createElement("input");
b.setAttribute("type","number");
b.setAttribute("id","billAmt");
b.setAttribute("name","number");
b.setAttribute("onkeyup","calc(this)");
cell1.appendChild(b);
var cell2 = row.insertCell(2);
var c = document.createElement("input");
c.setAttribute("type","date");
cell2.appendChild(c);
var cell3 = row.insertCell(3);
var d = document.createElement("input");
d.setAttribute("type","text");
cell3.appendChild(d);
var cell4 = row.insertCell(4);
var e = document.createElement("button");
e.innerText = "Delete";
e.setAttribute("id","delBtn");
e.setAttribute("onclick","removeRow(this)");
cell4.appendChild(e);
}
function removeRow(elem) {
var table = elem.parentNode.parentNode.parentNode;
var rowCount = table.rows.length;
var row = elem.parentNode.parentNode;
row.parentNode.removeChild(row);
}
var x = 0;
var y = 0;
var z = 0;
function calc(obj) {
var e = obj.id.toString();
if (e == 'startAmt') {
x = Number(obj.value);
y = Number(document.getElementById('billAmt').value);
} else {
x = Number(document.getElementById('startAmt').value);
y = Number(obj.value);
}
z = x + y;
document.getElementById('total').value = z;
document.getElementById('totalAmt').innerHTML = z;
}
</script>
</body>
</html>
結果我得到:
_「不知道爲什麼,但代碼無法在JSFiddle上運行」_ - [因爲您需要將wrap方法設置爲'
'](http://stackoverflow.com/q/7043649/4642212)。 – Xufox看起來你最終有多個具有相同ID的元素。如果你說'document.getElementById('billAmt')'我找到了10個,我應該返回哪一個?也許只有我找到的第一個?想想看。 – csmckelvey
你也可以通過定義你的函數,例如'window.calc = function(obj){' –