Javascript 未捕获范围错误:字符串长度无效

Javascript 未捕获范围错误:字符串长度无效,javascript,html,Javascript,Html,所以我刚刚开始学习javascript,实际上有两门课。所以我的知识非常有限。但我正在尝试创建一个约会应用程序,并且不断收到一个未捕获的rangeError:无效的字符串长度错误,我不知道为什么或者如何修复它。基本上,我已经得到了一些代码复制没有太多的解释,所以如果有人可以帮助我解决这个错误,它将不胜感激。下面是出现错误的代码,我相信是行table+=appt.tableRow();这是问题的根源。这段代码显然还有更多内容,但不确定是否需要给出,因为问题出在showTable函数中 编辑:我刚刚

所以我刚刚开始学习javascript,实际上有两门课。所以我的知识非常有限。但我正在尝试创建一个约会应用程序,并且不断收到一个未捕获的rangeError:无效的字符串长度错误,我不知道为什么或者如何修复它。基本上,我已经得到了一些代码复制没有太多的解释,所以如果有人可以帮助我解决这个错误,它将不胜感激。下面是出现错误的代码,我相信是行table+=appt.tableRow();这是问题的根源。这段代码显然还有更多内容,但不确定是否需要给出,因为问题出在showTable函数中

编辑:我刚刚添加了整个javascript代码以使其更简单

var Appointment = function(subject, description,date, time) {
this.subject = subject;
this.description = description;
this.datetime = new Date(date + " " + time);
this.completed = false;

};

Appointment.prototype.isDue = function(){
var now = new Date();
if(this.datetime > now){
    return false;
} else {
    return true;
}
};

Appointment.prototype.whenDue = function(){
return this.datetime -  new Date();
}

Appointment.prototype.toString = function(){
var s = this.subject +'\n'+
    this.datetime.toString() + '\n';
if(this.completed){
    s +="Not Completed\n\n";
}
return s
};

Appointment.prototype.howManyDaysTill = function() {
var ms = (this.datetime -  new Date()) / 24/60/60/1000
return ms;
};

Appointment.prototype.howManyHoursTill = function () {
var hours = (this.datetime - new Date()) /60/60/1000
return hours;
};

Appointment.prototype.getDate = function() {
return this.datetime.toDateString();
};

Appointment.prototype.getTime = function (){
return (this.datetime.getHours()) + ":" + (this.datetime.getMinutes());
};

Appointment.prototype.tableRow = function(){
var tr = "<tr><td>" + this.getDate() + "</td><td>" +
    this.getTime() + "</td><td>" + this.subject +
    "</td></tr>";
return tr;
};

var appointments = [];

window.onload = function(){
var newButton = document.getElementById("new");
newButton.onclick = function () {
    var subj = prompt("Enter a subject title for the appointment");
    var desc = prompt("Enter a description for the appointment");
    var date = prompt("Enter the appointment date in the format (e.g) 'Sep 
25, 2012");
    var time = prompt("Enter the appointment time in the format hh:mm");
    var a = new Appointment((subj,desc,date,time));
    appointments.push(a);
    return showTable();
};

var showTable = function() {
var tableDiv = document.getElementById("table"),
    table = "<table border='1'>" +
        "<thead><th>Date</th><th>Time</th><th>Subject</th><th>Completed</th> 
</thead>";
for (var i = 0, j = appointments.length; i < j; j++) {
    var appt = appointments[i];
    table += appt.tableRow();
}
table += "</table>";
tableDiv.innerHTML = table;
};


}
var约会=功能(主题、描述、日期、时间){
this.subject=主语;
this.description=描述;
this.datetime=新日期(日期+时间);
this.completed=false;
};
Appointment.prototype.isDue=函数(){
var now=新日期();
如果(this.datetime>now){
返回false;
}否则{
返回true;
}
};
Appointment.prototype.whenDue=函数(){
返回this.datetime-new Date();
}
Appointment.prototype.toString=函数(){
var s=this.subject+'\n'+
this.datetime.toString()+'\n';
如果(本文件已完成){
s+=“未完成\n\n”;
}
返回s
};
Appointment.prototype.howmanydaystal=函数(){
var ms=(this.datetime-new Date())/24/60/60/1000
返回ms;
};
Appointment.prototype.howManyHoursTill=函数(){
var hours=(this.datetime-new Date())/60/60/1000
返程时间;
};
Appointment.prototype.getDate=函数(){
返回此.datetime.toDateString();
};
Appointment.prototype.getTime=函数(){
return(this.datetime.getHours())+“:”+(this.datetime.getMinutes());
};
Appointment.prototype.tableRow=函数(){
var tr=”“+this.getDate()+“”+
this.getTime()+“”+this.subject+
"";
返回tr;
};
风险值=[];
window.onload=函数(){
var newButton=document.getElementById(“新”);
newButton.onclick=函数(){
var sub=提示(“为约会输入主题标题”);
var desc=提示(“输入约会描述”);
var date=提示(“以格式(例如)”输入约会日期”Sep
25, 2012");
var time=提示(“以hh:mm格式输入约会时间”);
var a=新任命((主体、描述、日期、时间));
任命。推动(a);
返回showTable();
};
var showTable=函数(){
var tableDiv=document.getElementById(“表”),
table=“”+
“DateTimeSubjectCompleted
";
对于(变量i=0,j=0.length;i
HTML5

 <!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="appointments.js"></script>
<title>Appointments</title>
</head>
<body>
<button id="new">New Appointment</button>
<div id ="table"></div>
<header>
    <h1>
        Appointments Book
    </h1>
    <p> Enter appointment details and press OK to add, Cancel to revert.</p>
</header>
<table>
<tr>
    <td>Subject : </td> <td>input type="text" size="40" id="subject"</td>
</tr>
<tr>
    <td>Description</td>
    <td>
        <textarea rows = "5" cols=""50" maxlength="200" id="description">
</textarea>
    </td>
</tr>
<tr> <td>Due Date:</td><td><input type ="date" id="duedate"/></td>
</tr>
</table>
<button id = "OK">OK </button><button id = "cancel">Cancel</button>
<hr/>
</body>
</html>

约会
新任命
预约簿
输入约会详细信息,然后按“确定”添加,按“取消”还原

主题:输入type=“text”size=“40”id=“主题” 描述
for循环必须在i上递增,而不是在j上递增。当前的一个正在导致无限循环,因此下面的一行正在创建一个字符串,该字符串太大,JS引擎无法处理,因此出现了错误

table += appt.tableRow();

您使用的是
appointment.length
,但我看不到您的
appointment
变量。谢谢,这个答案让它工作得很好。我已经试着解决这个问题3个小时了。你是最棒的,谢谢。请始终接受对您有用的答案,并投票选出对您有帮助的所有答案:)