我正在加载一个使用ajax的PHP。首先页面加载没有错误。当我在一个按钮上触发onclick事件时,我得到错误“Uncaught SyntaxError:Unexpected identifier”,它在php文件的第二行显示,即HTML头标记。
php文件:
<html>
<head>
<title>Staff Portal</title>
<link rel="stylesheet" type="text/css" href="Instructor.css">
<script type="text/javascript" src="InstLessons.js"></script>
</head>
<body onload="loadCourse()">
<div id="InstHeader">
<div id="bar">
<table id="inst_bar_table">
<tbody><tr>
<td id="inst_bar_detail"><a href="InstOptions.php">Options</a></td>
<td id="inst_bar_detail"><a href="logout.php">Logout</a></td><td>
</td></tr>
</tbody></table>
</div><br>
<h1 id="InstHeaderTitle">Pennco Tech Instructor Portal</h1><h1>
</h1>
</div>
<div id="MainBody">
<div id="course"><table id="InstClassTable"><tbody><tr><th id="classHeadTbl">Code</th><th id="classHeadTbl">Course</th><th id="classHeadTbl"># Students</th><th id="classHeadTbl">Start Date</th><th id="classHeadTbl">End Date</th></tr><tr><td id="classHeadDetailTbl">GEN 101</td><td id="classHeadDetailTbl">Computer Applications</td><td id="classHeadDetailTbl">7</td><td id="classHeadDetailTbl">05-06-15</td><td id="classHeadDetailTbl">07-07-15</td></tr></tbody></table></div>
<div id="detailsection">
<div id="InstMenu">
<ul id="InstMenuList">
<li><a href="InstMain.php" id="MenuLink">Select Class</a></li>
<li><a href="InstAttendance.php" id="MenuLink">Class Attendance</a></li>
<li><a href="InstClassAssignments.php" id="MenuLink">Class Assignments</a></li>
<li><a href="InstClassMsgboard.php" id="MenuLink">Message Board</a></li>
<li><a href="InstClassStudentData.php" id="MenuLink">Student Data</a></li>
<li><a href="InstClassOverView.php" id="MenuLink">Class Summary</a></li>
</ul>
</div>
<div id="instLessons">
<h3><u>Course Lessons</u></h3>
<br>
<table id="instLessonTable">
<tbody>
<tr>
<td id="classHeadDetailTbl">Career Services Assignments</td>
<td id="InstLessonButtonDetail">
<button id="InstLessonButton" onclick="getLessons(Career Services Assignments)">Select</button>
</td>
</tr>
<tr><td id="classHeadDetailTbl">Mitchell Final</td>
<td id="InstLessonButtonDetail"><button id="InstLessonButton" onclick="getLessons(Mitchell Final)">Select</button></td>
</tr>
<tr>
<td id="classHeadDetailTbl">Mitchell Hand-In Assignments</td>
<td id="InstLessonButtonDetail">
<button id="InstLessonButton" onclick="getLessons(Mitchell Hand-In Assignments)">Select</button></td>
</tr>
<tr>
<td id="classHeadDetailTbl">Mitchell Lab Test</td>
<td id="InstLessonButtonDetail">
<button id="InstLessonButton" onclick="getLessons(Mitchell Lab Test)">Select</button>
</td>
</tr>
<tr>
<td id="classHeadDetailTbl">Mitchell Midterm</td>
<td id="InstLessonButtonDetail">
<button id="InstLessonButton" onclick="getLessons(Mitchell Midterm)">Select</button>
</td>
</tr>
<tr>
<td id="classHeadDetailTbl">PC</td>
<td id="InstLessonButtonDetail">
<button id="InstLessonButton" onclick="getLessons(PC)">Select</button>
</td>
</tr>
</tbody>
</table>
</div>
<div id="instLessonMenu">
<br>
<button id="LessonMenuButton" onclick="SetPercentages()">Set Course Percentages</button><br><br>
<button id="LessonMenuButton" onclick="newLesson()">New Class Lesson</button><br><br>
<button id="LessonMenuButton" onclick="DelLesson()">Delete Class Lesson</button><br><br>
<button id="LessonMenuButton" onclick="IndivLesson()">New Individual Lesson</button><br><br>
<button id="LessonMenuButton" onclick="DelIndivLesson()">Delete Individual Lesson</button><br><br>
</div>
</div>
</div>
按钮onclick事件上的javascript函数:
function getLessons(lesson){
if(xmlHttpGetLessons.readyState==4 || xmlHttpGetLessons.readyState == 0){
var lesson_title = encodeURIComponent(lesson);
xmlHttpGetLessons.open("GET", 'InstListLessons.php?lesson ="' + lesson_title + '"',true);
xmlHttpGetLessons.send(null);
xmlHttpGetLessons.onreadystatechange = LessonGetServerResponse;
}
}
答案 0 :(得分:0)
您的JavaScript函数调用将导致Unexpected identifier
语法错误。
<button … onclick="getLessons(Career Services Assignments)">
单击该按钮时,将执行以下JavaScript并导致错误。
getLessons(Career Services Assignments)
您希望将函数参数设置为字符串,与您在JavaScript中通常使用的方式相同;通过将内容包装在引号字符中。 (选择单引号,因为我们将调用嵌入到HTML属性值中,该值使用双引号。)
getLessons('Career Services Assignments')
然后将其重新插入HTML:
<button … onclick="getLessons('Career Services Assignments')">