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

时间:2017-10-07 00:43:59

标签: javascript html5

所以我刚开始学习javascript,字面上有2个课程。所以我的知识非常有限。但我正在尝试预约应用程序,我一直收到一个未捕获的范围错误:无效的字符串长度错误,我不知道为什么或如何解决它。基本上我已经给了一些代码来复制而没有太多的解释,所以如果有人可以帮助我解决这个错误,将不胜感激。出现错误的代码如下,我相信它是行表+ = 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;
};


}

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>

1 个答案:

答案 0 :(得分:1)

for循环必须在i上递增而不在j上递增。当前的一个导致无限循环,因此以下行创建一个太大而无法由JS引擎处理的字符串,因此错误

table += appt.tableRow();