在javascript中粘贴链接

时间:2016-12-10 08:51:01

标签: java jquery ajax jsp spring-mvc

大家好!

我在Spring MVC应用程序中有脚本,它在表中添加了一个条目。

$(document).ready(function () {

    $('#saveSubject').submit(function (e) {
        $.post('/university/subjectAdd', $(this).serialize(), function (subject) {
            $('#subjectsTableResponse').last().append(
                    '<tr>' +
                    '<td align=\"center\">' + subject.title + '</td>' +
                    '<td align=\"center\">' + '<a href=\"c:url value=\'/subject/update/{'+subject.id+'}\'/>' + Update + '</a>'+'</td>'+
                    '<td align=\"center\">' + '<a href=\"c:url value=\'/subject/delete/{'+subject.id+'}\'/>' + Delete + '</a>'+'</td>'+
                    '</tr>'
            );
        });

        clearInputs();

        e.preventDefault();
    });
});

但是当您添加录制错误时需要相关链接

Uncaught ReferenceError: Update is not defined

我的表:

<div class="tableSubjects">
    <table border=2 bgcolor="#C1CDCD" id="subjectsTableResponse">
        <tr>
            <td align="center"><B>Предмет</B></td>
        </tr>
        <c:forEach items="${subjectList}" var="subject">
         <c:if test="${subject.deleted eq false}">
                <tr>
                 <td align="center">${subject.title}</td>
                  <td align="center">
                    <a href="<c:url value='/subject/update/${subject.id}' />">Update</a>
                </td>
                <td align="center">
                    <a href="<c:url value='/subject/delete/${subject.id}' />">Delete</a>
                </td>
             </tr>
            </c:if>
        </c:forEach>
    </table>
</div>

enter code here

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

您有两个问题:

  • 如果UpdateDelete应该是文字字符串,则应将它们括在引号中。

  • 为了将subject作为对象而不是JSON字符串传递,您必须将'json'的数据类型传递给$.post

&#13;
&#13;
$(document).ready(function () {

    $('#saveSubject').submit(function (e) {
        $.post('/university/subjectAdd', $(this).serialize(), function (subject) {
            $('#subjectsTableResponse').last().append(
                    '<tr>' +
                    '<td align=\"center\">' + subject.title + '</td>' +
                    '<td align=\"center\">' + '<a href=\"c:url value=\'/subject/update/{'+subject.id+'}\'/>Update</a>'+'</td>'+
                    '<td align=\"center\">' + '<a href=\"c:url value=\'/subject/delete/{'+subject.id+'}\'/>Delete</a>'+'</td>'+
                    '</tr>'
            );
        }, 'json');

        clearInputs();

        e.preventDefault();
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;