简体   繁体   中英

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. 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

...
{% 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.

In this case, you can add your script close to the tag and and call the addRecord function like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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