我不知道为什么会出现这个错误,我只是想把在索引页中工作的表单移到post页。我只是简单地移动了表单,但是没有工作;uncaughttypeerror:由于某种原因无法读取undefined(匿名函数)的属性'split'。在
{% crispy comment_form comment_form.helper %}
$(document).ready(function() {
$(document).on('submit', 'form', function(e){
e.preventDefault();
if($(this).parents("tr").length != 0) {
parent_id = $(this).parents("tr").attr("id").split("_")[1];
data_str = $(this).serialize() + "&parent_id=" + parent_id;
} else {
data_str = $(this).serialize();
}
$.ajax({
type:'POST',
url:'/comment/create/', // make sure , you are calling currect url
data:data_str,
success:function(json){
alert(json.message);
if(json.status==200){
var comment = json.comment.trim();
var user = json.user;
/// set `comment` and `user` using jquery to some element
if(!json.parent) {
$(comment).insertBefore('.table tr:first');
}
else {
$(comment).insertBefore('#comment_' + json.parent_id + ' #child_comment:first');
$(".replies").text("reply" + json.comment_count + "see");
}
}
},
error:function(response){
alert("some error occured. see console for detail");
}
});
});
我的表格
^{pr2}$
我的观点def comment_thread(request, id):
comment = Comment.objects.get(id=id)
comments = comment.post.commented_post.all()
for c in comments:
c.get_children()
hidden_data = {
"post_id" : comment.post.id,
"origin_path" : request.get_full_path,
"parent_id" : None
}
comment_form = CommentForm(hidden_data=hidden_data)
context = {
"comment": comment,
'comment_form':comment_form
}
return render(request, "comments/comment_thread.html", context)
编辑:{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
{{ comment.get_comment }} via {{ comment.user }} | {{ comment.timestamp|timesince }} ago {% if not comment.is_child %} {% for child in comment.get_children %} {{ child.get_comment }}via {{ child.user }} {% endfor %} {% crispy comment_form comment_form.helper %} {% endif %} |
$(document).ready(function() {
$(document).on('submit', 'form', function(e){
e.preventDefault();
if($(this).parents("tr").length != 0) {
parent_id = $(this).parents("tr").attr("id").split("_")[1];
data_str = $(this).serialize() + "&parent_id=" + parent_id;
} else {
data_str = $(this).serialize();
$.ajax({
type:'POST',
url:'/comment/create/', // make sure , you are calling currect url
data:data_str,
success:function(json){
alert(json.message);
if(json.status==200){
var comment = json.comment.trim();
var user = json.user;
/// set `comment` and `user` using jquery to some element
if(!json.parent) {
$(comment).insertBefore('.table tr:first');
}
else {
$(comment).insertBefore('#comment_' + json.parent_id + ' #child_comment:first');
$(".replies").text("reply" + json.comment_count + "view all");
}
}
},
error:function(response){
alert("some error occured. see console for detail");
}
});
});