2017-10-07 69 views
0

所以我剛開始學習JavaScript,字面上有2個類。所以我的知識非常有限。但我試圖做一個預約的應用程序,我不斷收到一個Uncaught rangeError:無效的字符串長度錯誤,我不知道爲什麼或如何解決它。基本上我已經給了一些代碼複製沒有太多的解釋,所以如果任何人都可以幫助我這個錯誤,將不勝感激。錯誤出現的代碼在下面,我相信它是行表+ = appt.tableRow();這是造成這個問題的原因。顯然有更多的這種代碼,但不能肯定是否需要給予,因爲這個問題是在showTable功能未捕獲的rangeError:無效的字符串長度

編輯:我只是說整個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> 
+0

使用了'appointments.length'但我不」不要在別處看到你的約會變量 – Gerardo

回答

0

for循環必須在i上遞增,而不是在j上遞增。目前一個是造成無限循環,因此下面一行是創建一個字符串,它是過大的JS引擎,因此錯誤處理

table += appt.tableRow(); 
+0

謝謝,這個答案使它完美地工作。一直試圖解決這個問題3個小時。你是最棒的。 – kmce

+0

謝謝。請始終接受爲您工作的答案,並且投票贊成所有幫助您的答案:) –

相關問題