Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法读取属性';拆分';未定义函数的类型(匿名函数)_Javascript_Jquery_Python_Django - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';拆分';未定义函数的类型(匿名函数)

Javascript 未捕获类型错误:无法读取属性';拆分';未定义函数的类型(匿名函数),javascript,jquery,python,django,Javascript,Jquery,Python,Django,我不知道为什么会出现这个错误,我只是尝试将在索引页面中工作的表单移动到post页面。我只是移动了表格,但没有工作;未捕获的TypeError:由于某些原因,无法读取未定义(匿名函数)的属性“split” <div> {% crispy comment_form comment_form.helper %} </div> <script> $(document).ready(funct

我不知道为什么会出现这个错误,我只是尝试将在索引页面中工作的表单移动到post页面。我只是移动了表格,但没有工作;未捕获的TypeError:由于某些原因,无法读取未定义(匿名函数)的属性“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");
        }
      });
    });
我的看法

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

通过{{comment.user}}{{comment.timestamp}timesince}}ago {%if not comment.is_child%}
    {comment.get_children%}
  • {{child.get_comment}} 通过{{child.user}
  • {%endfor%}
{%crispy comment\u form comment\u form.helper%} {%endif%} $(文档).ready(函数(){ $(文档).on('submit','form',function(e){ e、 预防默认值(); if($(this.parents(“tr”).length!=0){ parent_id=$(this).parents(“tr”).attr(“id”).split(“”)[1]; data_str=$(this).serialize()+“&parent_id=“+parent_id; }否则{ data_str=$(this).serialize(); $.ajax({ 类型:'POST', url:“/comment/create/”,//请确保您正在调用currect url 资料:资料来源:, 成功:函数(json){ 警报(json.message); if(json.status==200){ var comment=json.comment.trim(); var user=json.user; ///使用jquery将'comment'和'user'设置为某个元素 如果(!json.parent){ $(注释).insertBefore('.table tr:first'); } 否则{ $(comment).insertBefore('#comment'+json.parent_id+'#child_comment:first'); $(“.reply”).text(“reply”+json.comment_count+“查看全部”); } } }, 错误:函数(响应){ 警报(“发生了一些错误。有关详细信息,请参阅控制台”); } }); });
$(this).parents(“tr”).attr(“id”)
在您尝试设置
parent\u id
变量时返回undefined。我看到在您的视图中,您正在传递“parent\u id”:无。HTML是什么样子的?我认为一个元素应该有一个ID供jQuery获取,而您没有在视图中设置它。如果这样做,您可以检查
If($(this).parents('tr').attr('ID')){/*split in here*/}

检查
$(this.parents('tr').attr('ID'))
value未定义或在执行拆分之前未定义。@AvinashRaj-hmm不确定它为什么未定义…发布模板文件的内容。@AvinashRaj感谢您的回复我已使用模板文件编辑了ted问题,{{comment.get_comment}是父项hmm我已使用模板文件编辑了ted问题,{{comment.get_comment}父对象是否像您没有在父对象TR上设置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");
        }
      });
    });