Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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 Django、Ajax和JS。当我提交评论时,如何防止页面重新加载和跳转到页面顶部_Javascript_Html_Python 3.x_Django - Fatal编程技术网

Javascript Django、Ajax和JS。当我提交评论时,如何防止页面重新加载和跳转到页面顶部

Javascript Django、Ajax和JS。当我提交评论时,如何防止页面重新加载和跳转到页面顶部,javascript,html,python-3.x,django,Javascript,Html,Python 3.x,Django,我遵循了一个解决方案,即如何让Django评论表单在不重新加载页面和跳转到页面顶部的情况下提交。我尝试了许多在线和离线解决方案,但仍然没有解决方案 表单工作正常,唯一的问题是提交时重新加载页面 我是Django后端和Ajax新手,如果有人能帮助我解决这个问题,我会很高兴。提前谢谢 jsajax $(文档).ready(函数(){ $(`.comment表单${post_id}`).提交(函数(){ $.ajax({ 数据:$(this).serialize(), 类型:$(this.attr(

我遵循了一个解决方案,即如何让Django评论表单在不重新加载页面和跳转到页面顶部的情况下提交。我尝试了许多在线和离线解决方案,但仍然没有解决方案

表单工作正常,唯一的问题是提交时重新加载页面

我是Django后端和Ajax新手,如果有人能帮助我解决这个问题,我会很高兴。提前谢谢

jsajax
$(文档).ready(函数(){
$(`.comment表单${post_id}`).提交(函数(){
$.ajax({
数据:$(this).serialize(),
类型:$(this.attr('method'),
url:$(this.attr('action'),
成功:功能(响应){
$();
},
错误:函数(响应){
console.log('错误',响应)
}
});
返回false;
});

});
通过添加
e.preventDefault()来防止页面重新加载


通过添加
e.preventDefault(),可以防止页面重新加载



你能分享注释模型代码吗?这在任何方面对你都有帮助吗?如果你指定哪些有效,哪些无效,这将很有帮助work@AjayLingayat我现在添加了模型代码。谢谢你的链接,我会查看的。@ha neul一切都很好,唯一的问题是当我提交评论时页面会重新加载,我想防止这种情况发生。@Moalanth看到我的答案。你能分享评论模型代码吗?这在任何方面对你都有帮助吗?如果你指定哪些有效,哪些无效,这将很有帮助work@AjayLingayat我现在添加了模型代码。谢谢你的链接,我会去看看。@ha neul一切都很好,唯一的问题是,当我提交评论时,页面会重新加载,我想防止这种情况发生。@Moalanth请查看我的答案。尝试过,但在我提交评论时页面仍会重新加载。打开控制台查看它们是否有错误,并将此函数从就绪函数中删除这是我从控制台获得的结果:未捕获引用错误:post_id未在HTMLDocument中定义。((索引):568)检查您的帖子id是否正确返回(
.comment form${post_id}
)到(“.comment form”)仅使用类名,但现在表单除了防止默认行为之外没有做任何事情。尝试过,但在我提交注释时仍会重新加载页面。打开控制台查看其是否存在错误,并将此函数从就绪函数中删除这是我从控制台获得的结果:未捕获引用错误:未定义post_idHTMLDocument。((索引):568)检查您的post id是否正确返回。我已将(
.comment form${post_id}
)更改为('.comment form'),仅使用类名,但现在该表单除了防止默认行为外,没有做任何事情。
$(`.comment-form${post_id}`).submit(function(e) { // Note here too, on submit a function in created where we catch e
      e.preventDefault(); // Here it is e is an event which is passed on clicking button
      $.ajax({
          data: $(this).serialize(), 
          type: $(this).attr('method'), 
          url: $(this).attr('action'), 
          success: function(response) {
              $();
          },
          error: function(response) {
            console.log('error', response)
         }
      });
      return false;
    });