gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'split' of undefined(anonymous function)

转载 作者:行者123 更新时间:2023-12-01 05:31:57 25 4
gpt4 key购买 nike

我不确定为什么会收到此错误,我只是试图将索引页面中正在使用的表单移动到发布页面。我只是移动了表格但没有工作;未捕获的类型错误:由于某种原因无法读取未定义(匿名函数)的属性“split”。

            <div>

{% crispy comment_form comment_form.helper %}

</div>

<script>
$(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");
}
});
});

我的表格

class CommentForm(forms.Form):
comment = forms.CharField(
widget=forms.Textarea(attrs={"placeholder": "leave"})
)
#hidden_field = forms.CharField(widget=forms.HiddenInput())

def __init__(self, hidden_data=None, data=None, files=None, **kwargs):
super(CommentForm, self).__init__(data, files, kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False
self.helper.add_input(Submit('submit', css_class='btn btn-default'))
if hidden_data:
self.helper.add_input(Hidden('post_id', hidden_data['post_id']))
self.helper.add_input(Hidden('origin_path', hidden_data['origin_path']))
if hidden_data.get('parent_id', None):
self.helper.add_input(Hidden('parent_id', hidden_data['parent_id']))
\

我的看法

  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 %}

<a href="{{ comment.get_origin }}">Go Back</a>

<table class='table'>

<tr><td>{{ comment.get_comment }}
<br/><small>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago </small>
{% if not comment.is_child %}
<ul>
{% for child in comment.get_children %}
<li>{{ child.get_comment }}
<small>via {{ child.user }}</small>


</li>
{% endfor %}
</ul>
<div>

{% crispy comment_form comment_form.helper %}

</div>
{% endif %}


</td></tr>


</table>

<script>
$(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");
}
});
});

最佳答案

当您尝试设置 parent_id 变量时,

$(this).parents("tr").attr("id") 返回未定义。我发现在您看来,您传递的是“parent_id”:无。 HTML 是什么样子的?我认为元素应该有一个 ID 供 jQuery 获取,而您没有在 View 中设置它。如果这是有意的,您可以检查 if ($(this).parents('tr').attr('id')) {/* split in here */}

关于javascript - 未捕获的类型错误 : Cannot read property 'split' of undefined(anonymous function),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com