简体   繁体   English

使用纯JavaScript移入文件时,jQuery脚本不起作用

[英]jQuery script doesn't work when moved into a file with plain javascript

I have functioning javascript and jQuery files. 我有正常运行的javascriptjQuery文件。 I tried copying the jQuery AJAX into my other file, but It doesn't work. 我尝试将jQuery AJAX复制到其他文件中,但是不起作用。 Instead of AJAX ing, it shows me the JSON info returned without the the template it was in. ? 它向我显示了返回的JSON信息,而不包含其所在的模板,而不是AJAX

View 视图

def customer_profile(request, pk):
    name = get_object_or_404(CEName, id=pk)
    name_form = NameForm(instance=name)
    return render(
        request,
        'customer/customer_profile.html',
        {'name':name, 'NameForm': name_form}
    )


def update_profile(request, pk):
    if request.POST:
        name = get_object_or_404(CEName, id=pk)
        name_form = NameForm(data=request.POST, instance=name)
        if name_form.is_valid():
            name_form.save()
            updated_name = get_object_or_404(CEName, id=name.id)
            name_json = render_to_string(
                'ce_profiles/name_json.html',
                {'name_json': updated_name,}
            )
            return JsonResponse({'name_json': name_json})

templates 模板

{% extends 'base.html' %}
{% load static %}

{% block content %}
  {% include 'ce_profiles/name_header.html' %}
  <div id="name" class="container d-flex justify-content-between pt-1">
    <div id="name_json">
      {{ name }}
    </div>
    <button id="update_button" class="bold btn btn-main btn-sm button-main">UPDATE</button>
  </div>
  <div id="div_NameForm" class="container" style="display: none;">
    <hr size="3px">
    <form id="NameForm" method="POST" action="{% url 'customer:update-profile' name.id %}">
      {% csrf_token %}
      {{ NameForm.as_p }}
      <br>
      <button id="save_changes" type="submit" class="btn btn-main button-main btn-block">Save Changes</button>
    </form>
  </div>
{% endblock %}

{% block script %}
  <script src="{% static 'ce_profiles/ce_profiles.js' %}"></script>
  <script src="{% static 'ce_profiles/ce_profiles_jquery.js' %}"></script>
{% endblock %}

<div id="name_json">
  {{ name_json }}
</div>

jQuery jQuery的

$(document).ready(function() {
  $("#NameForm").submit(function(event) {
    event.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: $(this).attr('method'),
      data: $(this).serialize(),
      dataType: 'json',
      success: function(data) {$("#name_json").html(data.name_json)}
    });
  });
});

javascript JavaScript的

$(document).ready(function() {
  $("#NameForm").submit(function(event) {
    event.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: $(this).attr('method'),
      data: $(this).serialize(),
      dataType: 'json',
      success: function(data) {$("#name_json").html(data.name_json)}
    });
  });
});


let updateButton
let toggleNameForm = function(e) {
  e.stopPropagation();
  let x = document.getElementById("div_NameForm");
  let btn = this;
  if (x.style.display === "none") {
    x.style.display = "block";
    btn.innerHTML = "CANCEL";
    btn.classList.replace("button-main", "button-secondary");
  } else {
    x.style.display = "none";
    btn.innerHTML = "UPDATE";
    btn.classList.replace("button-secondary", "button-main");
  }
};


document.addEventListener('DOMContentLoaded', function () {
  updateButton = document.getElementById('update_button');
  updateButton.addEventListener('click', toggleNameForm);
});

When I delete the script link for ce_profiles_jquery.js and attempt to update the name displayed, all that shows in the browser is: name_json: "<div id\\"name_json\\">\\n The Name\\n</div>" 当我删除ce_profiles_jquery.jsscript链接并尝试更新显示的名称时,浏览器中显示的所有内容是: name_json: "<div id\\"name_json\\">\\n The Name\\n</div>"

??? ???

Check that the correct javascript file is being referenced in your template by checking the source in your browser. 通过检查浏览器中的源,检查模板中是否引用了正确的javascript文件。

I ran your code in a test environment and everything works correctly. 我在测试环境中运行了您的代码,一切正常。 When I commented out your jQuery code, the form submitted normally and the subsequent page displayed: 当我注释掉您的jQuery代码时,表单正常提交,随后显示以下页面:

{name_json: "<div id=\"name_json\">The Name</div>"}

If you are not receiving any errors in your browser console, your reference to {% static 'ce_profiles/ce_profiles.js' %} is probably a different file without the jQuery code. 如果您在浏览器控制台中未收到任何错误,则对{% static 'ce_profiles/ce_profiles.js' %}引用可能是没有jQuery代码的其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM