此代码描述了选择课程中的科目 HTML代码
<form action="mode" onSubmit="calcRoute();return false;" id="keywordForm">
<table border="0">
<thead>
<tr>
<td>COURSE</td>
<td> </td>
<td>SUBJECTS</td>
<td> </td>
<td>TOPIC</td>
</tr>
</thead>
<tr>
<td>
<select id="courseName" name="courseName" multiple size="10">
<c:forEach items="${courses}" var="course">
<option value="${course.courseId}">${course.courseName}</option>
</c:forEach>
</select>
</td>
<td> </td>
<td>
<select id="subjectName" name="subjectName" multiple size="10">
</select>
</td>
<td> </td>
<td>
<select id="topicName" name="topicName" multiple size="10">
</select>
</td> <!-- /controls -->
<td>
<button id="submit"><i class="icon-chevron-right"></i></button>
</td>
</tr>
<div id="alert"></div>
<!-- /controls -->
</table>
</form>
Ajax代码
$('#courseName').change(function () {
var courseId = $(this).val().toString().replace("[", "");
// var courseName = $(this).val().toString().replace("[", "");
$.ajax({
type: "POST",
url: "/sil-web/student/select-subjects?courseId=" + courseId,
//url: "/sil-web/student/select-subjects?courseName=" + courseName,
contentType: 'application/json',
success: function (response) {
$("#subjectName").children().remove();
$.each(response, function (index, item) {
console.log("subject" + item.subjectId);
$("#subjectName").append("<option value=" + item.subjectId + ">" + item.subjectName + "</option>");
});
}
});
});
此标签主题名称未完全采用
$("#subjectName").append("<option value=" + item.subjectId + ">" + item.subjectName + "</option>"
就像
一样<option value="advanced" algorithms="">advanced algorithms</option>
因为我认为课程名称只是高级算法,因为它不是该课程下的显示科目如何解决这个问题?
答案 0 :(得分:1)
试试这个:
$("#subjectName").append("<option value='" + item.subjectId + "'>" + item.subjectName + "</option>"
答案 1 :(得分:1)
我认为这与这篇文章有关: Escape quotes in JavaScript 你有一个“在你的subjectId。你必须通过这样做将它转换为HTML代码:
item.subjectId.replace(/"/g, '"')