在异常TypeError上暂停:document.getElementById(...)为null

艾哈迈德·亚辛(Ahmed Yasin)

好吧,我在这里创建评论系统。如果已经有至少一条评论,但是如果没有评论并尝试创建一条评论,则它会正常工作,这比向我显示在TypeError异常上暂停的错误:document.getElementById(...)为null我不知道该如何解决。

html

  <div id="post_id" post-id="{{post.pk}}" post-slug="{{post.slug}}">    
        {% if node.level < 3 %} 
        <button class='btn btn-success' onclick="myFunction({{node.id}})">Reply</button>
        {% endif %}
        </div> 

jQuery,ajax功能

$(document).on('click', '#newcomment, #newcommentinner', function (e) {
    e.preventDefault();

    var button = $(this).attr("value");
    var post_id = document.getElementById('post_id').getAttribute('post-id'); #Here is an error appearing.
    var post_slug = document.getElementById('post_id').getAttribute('post-slug');
    console.log(post_id,'postid')
    var placement = "commentform"
    if (button == "newcommentform") {
      var placement = "newcommentform"
    }
 
    $.ajax({
      type: 'POST',
      url: '{% url "posts:addcomment" %}',
      data: $("#" + button).serialize() + "&post_id="+post_id + "&post_slug="+post_slug,
      cache: false,
      error: console.log('post_id' + post_id),
      success: function (json) {
        console.log(json)

        $('<div id="" class="my-2 p-2" style="border: 1px solid grey"> \
          <div class="d-flex justify-content-between">By ' + json['user'] + '<div></div>Posted: Just now!</div> \
          <div>' + json['result2'] + '</div> \
          <hr> \
          </div>').insertBefore('#' + placement);

        $('.commentform').trigger("reset");
        formExit()
      },
      error: function (xhr, errmsg, err) {
      }
    });
  })

如果需要更多的代码而不是在评论会话中告诉我。我将用该信息更新我的问题。

亚历山大·Showlsky

我不知道您为什么要用纯JS弄乱jQuery synthax?:)var post_id = $('#post_id').attr('post-id')应该做到这一点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:使用JSP的getElementById为null

TypeError:document.getElementById(...)为null不会停止出现

我找不到错误?TypeError:document.getElementById(...)为null

TypeError:document.getElementById(...)在javascript中为null

Ajax Uncaught TypeError: document.getElementById(...) is null

如何将 document.getElementById(...) is null 设置为 true

Android WebView始终在loadUrl上为javascript getElementById返回null

document.getElementById() 为空

document.getElementById()返回null

document.getElementById返回null

Javascript Document.getElementById返回null

离子2 document.getElementById返回null

document.getElementById 始终返回 null

所有 document.getElementById('') 返回 null

document.getelementbyid在循环中返回null

从document.getElementById()。value获取null

document.getElementById为null?...我的代码有什么问题..?

Vuejs + Google Maps> document.getElementById,this.refs为div返回null

document.getElementById(“ elementId”)。innerHTML =()的倍数,导致第二个返回为null

Selenium-Java-geckodriver:由于“ document.getElementById(...)为空”,测试执行因JavaScript错误而暂停

使用.getElementById捕获元素时为Null

document.getElementById为null和/或不起作用,即使它在那里并且在加载之后也是如此

document.getElementById()。value如果为0,则将秒数截断

未为“ document.getElementById()”定义文档错误

标记错误:document.getElementById(...) 为空或不是对象

以 php 变量为 id 回显 document.getElementById()

Javascript:TypeError:document.getElementById(...)为空动态创建的<div> MVC4

document.getElementById()!= null在Firefox上不起作用

为什么document.getElementById在这里返回null?