[英]Uncaught ReferenceError: addRecord is not defined at HTMLButtonElement.onclick
when i click on the add record button it doesn't work and then it gives me an uncaught reference error, but if i load one of the jquery https://code.jquery.com/jquery-3.4.1.min.js scripts in the browser, it shows the button working perfectly.当我点击添加记录按钮时它不起作用,然后它给了我一个未捕获的引用错误,但是如果我加载一个 jquery https://code.jquery.com/jquery-3.4.1.min.js浏览器中的脚本,它显示按钮工作正常。 any suggestions please?
请问有什么建议吗?
base.html基本文件
<!DOCTYPE html >
{%load staticfiles%}
{% load crispy_forms_tags %}
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'style.css' %}"/>
<title>{% block title %}{% endblock %}</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body class="container-fluid">
<div class="row">
<div class="col-lg-12">
{% block content %}
{% endblock %}
</div>
</div>
</body>
</html>
okay.html好的.html
...
{% extends "s/base.html" %}
{% load staticfiles %}
{% block content %}
{% block s_base %}
{{ block.super }}
<script>
alert('Helloo');
function addRecord(){
console.log('hhhffhgfhdf');
}
</script>
{% endblock %}
<div> <a href="javascript:void(0);" ><button type="button" class="btn btn-primary" onclick="addRecord();"> Add New Advert </button></a></div>
{% endblock %}
it seems like you don't want to use Jquery, I suppose.我想您似乎不想使用 Jquery。
In this case, you can add your script close to the tag and and call the addRecord function like:在这种情况下,您可以将脚本添加到标签附近并调用 addRecord 函数,例如:
window.onload = function(){
var button = document.querySelector('button');
button.onclic k= function(e){
e.preventDefault();
addRecord()
}
}
There's several ways of doing it but this is just an idea.有几种方法可以做到,但这只是一个想法。 remember to remove the
onclick
attibute from your html button记得从你的 html 按钮中删除
onclick
属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.