-1
我试图创建一个简单的费用跟踪器页面,使用非常少量的引导程序(对于我的按钮)。但是当我按下按钮来添加一个新的行到我的表,我在控制台中出现以下错误。我有我的js文件中的功能,但无论我做什么,我都无法弄清楚什么是错的。 HTML:referenceError:<function>未定义
<!DOCTYPE html>
<html>
<script type="text/javascript" src="expenses.js"></script>
<head>
<meta charset = "utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap /3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<title>Expenses</title>
</head>
<body>
<div class="dataEntry">
<input type="date" id="inputdate" name = "date" value=""></input>
<input type="text" id = "inputstore" name = "store" value="Store"></input>
<input type="text" id = "inputcategory" name = "category" value="Category"> </input>
<input type="text" id = "inputitem" name = "item" value="Item"></input>
<input type="text" id = "inputamount" name = "amount" value="Amount"></input>
<button type="button" class="btn btn-primary" name="button" \
onclick="newExpense(inputdate,inputstore,inputcategory,inputitem,inputamount)"> Enter</button>
<table id="dataTable">
<tr>
<th>Date</th>
<th>Store</th>
<th>Category</th>
<th>Item</th>
<th>Amount</th>
</tr>
</table>
</div>
`
和Javascript:
"use strict"
class Expense {
constructor(d,s,c,i,a){
self.date = d;
self.store = s;
self.cat = c;
self.item = i;
self.amount = parseFloat(a);
}
buildTable() {
let row = table.createElement('tr');
for (let cell of [self.date, self.store, self.cat, self.item, self.amount]){
let el = tr.createElement('td');
row.appendChild(el);
}
}
}
class ExpenseDB {
constructor() {
this.array = [];
}
static newExpense(date, store, cat, item, amount) {
let expense = new Expense(date,store,cat,item,amount);
return expense;
}
}
应该是'ExpenseDB.newExpense(...)'。无论如何,这只会创建一个新的对象,什么也不做。另外,你正在传递元素(通过全局ID(坏)) - 你需要元素的值! – Li357
哪里有全球ID? 'let'声明使得块的所有变量都是局部的。 – Allen
'inputdate,inputstore,inputitem,inputamount'等 – Li357