我想从输入字段(数字)和单选按钮中获取值,并将其存储到具有当前日期的数组中。然后我希望能够在一个 div 中获取和显示总数,并在另一个 div 中列出该数组的所有值。
到目前为止,这是我的 HTML:
<div data-role="fieldcontain" data-controltype="textinput">
<input name="" id="amount" placeholder="Amount" value="" type="number">
</div>
<div data-role="fieldcontain" data-controltype="radiobuttons">
<fieldset data-role="controlgroup" data-type="horizontal" id="type">
<input id="radio1" name="" value="cash" type="radio">
<label for="radio1">cash</label>
<input id="radio2" name="" value="cheque" type="radio">
<label for="radio2">cheque</label>
</fieldset>
</div>
<button onClick="Transaction.add()">ADD</button>
<button onClick="Transaction.subtract()">SUBTRACT</button>
<div>
<button onClick="Transaction.getTotal()">GET TOTAL!</button>
<p id="total"></p>
</div>
<div>
<button onClick="Transaction.list()">List</button>
<p id="listSpace"></p>
</div>
和我的 JS:
function currentDate() {
var d = new Date();
var dateString = d.getDate() + "/" + d.getMonth + 1 + "/" + date.getFullYear().toString().substr(2, 2) + " " + d.getHours() + minutes
return dateString;
}
var movements = [];
function Transaction() {
this.amount = Number(document.getElementById("amount").value);
this.date = currentDate();
this.type = document.getElementById("type").value;
this.add = function () {
movements.push([this.amount, this.date, this.type]);
};
this.subtract = function () {
movements.push([-(this.amount), this.date, this.type]);
};
this.getTotal = function () {
for (var i = 0; i < movements.length; i++) {
movements[i][0].reduce(function (a, b) {
document.getElementById("total").innerHTML = a + b;
}, 0);
}
};
this.list = function () {
for (var j in movements) {
document.getElementById("listSpace").innerHTML = movements[j]
};
};
}
我知道我作为一个新手可能做错了很多事情,所以任何帮助解决这个问题将不胜感激!
这是 JSfiddle 链接:http: //jsfiddle.net/soundCat/93Jsz/