split函数python 未定义_Uncaught TypeError:无法读取未定义的属性“split”(匿名函数)...

在尝试使用JavaScript处理表单提交并触发AJAX请求时遇到了一个错误:Uncaught TypeError: 无法读取未定义的属性'split'。错误发生在尝试获取父元素ID并进行分割操作的代码段。问题可能源于父元素ID未正确设置或jQuery选择器未找到匹配项。检查DOM结构和相关ID,确保在调用split之前变量已定义。
摘要由CSDN通过智能技术生成

我不知道为什么会出现这个错误,我只是想把在索引页中工作的表单移到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 %}

Go Back

{{ 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");

}

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值