单击按钮时如何解决未定义的函数错误

时间:2019-06-02 03:57:24

标签: javascript html flask

单击按钮时,我使用下面的代码段调用函数

<a class="btn btn-primary btn-sm" role="button"  style="display: block; margin: 0 auto;" onclick="RecordFunction()">Record</a>

该函数在下面定义为

<script type="text/javascript">
function RecordFunction (){
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "/record/{{name}}", true);
  xhttp.send();
  alert("Thank you ");
}
</script>

但是它抛出了错误

  

未捕获的ReferenceError:未定义RecordFunction       在HTMLAnchorElement.onclick

当我搜索错误时,我发现这可能是因为该函数未加载,所以我尝试在按钮div之前定义它,并尝试 this。但是没有用。任何帮助表示赞赏。

我的整个index.html如下所示:

<!doctype html>
<html>

    <head>    

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

  </head>
   <body>

<div class="jumbotron text-xs-center">
  <h1 class="display-3">Thank You {{ name }}</h1>
  <p class="lead"  style"text-align:center"><strong>click the below button </strong></p>
  <hr>
  <div class="col-md-4"></div>
  <div class="col-md-6">
  <p class="lead">
    <a class="btn btn-primary btn-sm" role="button"  style="display: block; margin: 0 auto;" onclick="RecordFunction()"> Give Consent  </a>
  </p>
</div>

<div class="col-md-4"></div>
</div>
<!-- </div> -->
<div class="bgImgCenter"></div>
<script type="text/javascript">
function RecordFunction (){
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "/record/{{name}}", true);
  xhttp.send();
  alert("Thank You ");
}
</script>

   </body>
</html>

我正在用烧瓶模板渲染

@app.route('/<user>')
def hello(user):
    name=user
    return render_template('index.html', name = name)

1 个答案:

答案 0 :(得分:0)

尝试一下:

QT = core widgets network networkauth requires(qtConfig(tableview)) CONFIG -= app_bundle LIBS += -LC:/OpenSSL-Win64/lib -lubsec HEADERS += \ twitter.h \ twittertimelinemodel.h INCLUDEPATH += C:/OpenSSL-Win64/include SOURCES += \ main.cpp \ twitter.cpp \ twittertimelinemodel.cpp FORMS += \ twitterdialog.ui # install target.path = $$[QT_INSTALL_EXAMPLES]/oauth/twittertimeline INSTALLS += target

缺少a =,应该是

<p class="lead" style"text-align:center"><strong>click the below button </strong></p>