【问题标题】:Uncaught ReferenceError: deleteNote is not defined at HTMLButtonElement.onclick未捕获的 ReferenceError:deleteNote 未在 HTMLButtonElement.onclick 中定义
【发布时间】:2022-02-02 14:06:01
【问题描述】:

所以,我刚刚学习了用烧瓶制作一些网站。一切都很好,直到这个错误让我发疯。你能解决我的问题吗?

这是我删除一些笔记的 def 函数

@views.route('/delete-note',methods=['POST']) 
def delete_note():
note = json.loads(request.data)
noteId = note['noteId']
note = Note.query.get(noteId)
if note:
    if note.user_id == current_user.id:
        db.session.delete(noteId)
        db.session.commit()
return jsonify({})

这是我的 .js 代码

function deleteNote(noteId) {
  fetch("/delete-note", {
    method: "POST",
    body: JSON.stringify({ noteId: noteId }),
  }).then((_res) => {
    window.location.href = "/";
  });
}

这就是我用html制作按钮的方式

<ul class="list-group list-group-flush" id="notes">
  {% for note in user.notes %}
  <li class="list-group-item">
    {{ note.data }}
    <button type="button" class="close" onClick="deleteNote({{ note.id }})">
      <span aria-hidden="true">&times;</span>
    </button>
  </li>
  {% endfor %}
</ul>

你能帮帮我吗?我不知道如何解决它。请帮帮我

【问题讨论】:

  • 你加载的js文件是否正确。您可以尝试在 HTML 标记之前加载该函数,例如

标签: javascript python html


【解决方案1】:

您是否正确加载了 js 文件。您可以尝试在 HTML 标记之前加载该函数,例如

<head>
 <script>
  function deleteNote(noteId) {
  fetch("/delete-note", {
    method: "POST",
    body: JSON.stringify({ noteId: noteId }),
  }).then((_res) => {
    window.location.href = "/";
  });
}</script>
</head>

<body>
<ul class="list-group list-group-flush" id="notes">
 {% for note in user.notes %}
 <li class="list-group-item">
 {{ note.data }}
<button type="button" class="close" onClick="deleteNote({{ note.id }})">
  <span aria-hidden="true">&times;</span>
</button>
</li>
 {% endfor %}
 </ul>
</body>

【讨论】:

  • 所以,我必须把我的 js 代码放到我的 html 中吗?
  • 我已经在我的 base.html 上调用了它,但我把它放在身体上而不是头上
  • 别介意兄弟,找到错误在哪里。谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2020-07-07
  • 2017-05-22
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多