如何在Ajax中使用标签正则表达式?

问题描述 投票:0回答:1

我已经在base.html中进行了井号工作,就像有人用#something键入内容一样,它将替换为html中带有链接的javascript。它正在发布列表中。所以我想对它进行评论。但是注释具有ajax方法,这就是为什么注释中不起作用的原因。我们可以同时保留它们吗(AJAX和Hashtag)。

我的base.html:

$(document).ready(function() {
      $("p").each(function(data) {
        var strText = $(this).html();
        console.log('1. strText=', strText);
        var arrElems = strText.match(/@[a-zA-Z0-9]+/g);
        console.log('arrElems=', arrElems);
        $.each(arrElems, function(index, value){
            strText = strText.toString().replace(value, '<a href="/user/'+value.replace('@', '')+'">'+value+'</a>');
        });
        console.log('2. strText=', strText);
        $(this).html(strText);
      });
    });
   (#the ajax method for comments)
$(document).on('submit', '.comment-form', function(event){
       event.preventDefault();
       console.log($(this).serialize());
        $("p").each(function(data) {
        var strText = $(this).html();
        console.log('1. strText=', strText);
        var arrElems = strText.match(/@[a-zA-Z0-9]+/g);
        console.log('arrElems=', arrElems);
        $.each(arrElems, function(index, value){
            strText = strText.toString().replace(value, '<a href="/user/'+value.replace('@', '')+'">'+value+'</a>');
        });
        console.log('2. strText=', strText);
        $(this).html(strText);
      });
    });
       $.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          cache: false,
          data: $(this).serialize(),
          dataType: 'Json',
          success: function(response) {
            $('.main-comment-section').html(response['form']);
            $('textarea').val('');
            $('.reply-btn').click(function() {
               $(this).parent().parent().next('.replied-comments').fadeToggle()
               $('textarea').val('');
            });
          },
          error: function(rs, e) {
            console.log(rs.responseText)
          },

       });
    });

我的comments.html:

<form method="post" enctype="multipart/form-data" class="comment-form" action=".">
{% csrf_token %}
{{ comment_form.as_p }}
<input type="submit" value="submit" class="btn-btn-outline-success">
</form>
<div class="container">
    {{ comments.count }} comment{{ comments|pluralize }}
    {% for comment in comments %}
    <blockquote class="blockquote">
      <p class="mb-0">{{ comment.content }}</p>
        <div class="options">
          {% if comment.user == user %}
          <a href="{% url 'comment-delete' pk=comment.pk %}">delete</a>
          {% endif %}
        </div>
      <footer class="blockquote-footer">by <cite title="Source Title">{{ comment.user }}</cite>
        <button type="button" name="button" class="reply-btn btn btn-outline-dark btn-sm">reply</button> 

      </footer>
</blockquote>
<div class="replied-comments container mt-2" style="display:none;">
    {% for reply in comment.replies.all %}
    <blockquote class="blockquote">
      <p class="mb-0"><small>{{ reply.content }}</small></p>
      <footer class="blockquote-footer"><small>by <cite title="Source Title">{{ reply.user }}</cite></small></footer>
    </blockquote>
    {% endfor %}
    <div class="form-group-row">
        <form method="post" class="reply-form" action="." enctype='multipart/form-data'>
             {% csrf_token %}
             <input type="hidden" name="comment_id" value="{{ comment.id }}">
             {{ comment_form.as_p }}
             <input type="submit" value="submit" class="btn-btn-outline-success">
         </form>
    </div>
</div>
{% endfor %}

ajax方法会引发错误,例如ajax不起作用,#标签不起作用。

javascript django ajax django-forms ajaxform
1个回答
0
投票

好吧,我只需要将脚本放入comment.html中,而不要放在base.html中。

© www.soinside.com 2019 - 2024. All rights reserved.